Search code examples
javaxmljcomboboxdom4j

Populating JComboBox with XML attribute - DOM4J


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.

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

Here's a sample of my XML file:

<?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)


Solution

  • 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());
    }