使用XML属性填充JComboBox - DOM4J

时间:2022-04-12 23:15:45

I was trying to use DOM4J API to populate a JComboBox with a XML file attributes, but I don't know how it's done.

我试图使用DOM4J API来填充具有XML文件属性的JComboBox,但我不知道它是如何完成的。

I've been reading the API documentation but I still can't do it. Can you help me?

我一直在阅读API文档,但我仍然无法做到。你可以帮我吗?

Here's a sample of my XML file:

这是我的XML文件的示例:

<?xml version="1.0"?>
<components>
    <resources id="House">
            <id>int</id>
            <type>string</type>
            <maxUsage>float</maxUsage>
            <minUsage>float</minUsage>
            <averageUsage>float</averageUsage>
    </resources>
    <resources id="Commerce">
            <id>int</id>
            <type>string</type>
            <maxUsage>float</maxUsage>
            <minUsage>float</minUsage>
            <averageUsage>float</averageUsage>
    </resources>
</components>

EDIT: I need a JComboBox that show: House, Commerce, etc etc (the content of the id attribute)

编辑:我需要一个JComboBox显示:房子,商业等等(id属性的内容)

1 个解决方案

#1


2  

You might do something like this:

你可能会这样做:

List list = document.selectNodes("//resources/@id" ); //using xpath
Iterator iter=list.iterator();
while(iter.hasNext()){
    Attribute attribute=(Attribute)iter.next();
    jCombo.addItem(attribute.getValue());
}

#1


2  

You might do something like this:

你可能会这样做:

List list = document.selectNodes("//resources/@id" ); //using xpath
Iterator iter=list.iterator();
while(iter.hasNext()){
    Attribute attribute=(Attribute)iter.next();
    jCombo.addItem(attribute.getValue());
}