Dante
Dante

Reputation: 297

parse XML with multiple attributes to list

I have an XMl file like this:

<?xml version="1.0" encoding="UTF-8"?>
<subtests>
        <subtest id="Detect" name="Device detection" />
        <subtest id="DeviceType" name="Device type" />
        <subtest id="VendorName" name="Vendor" />
        <subtest id="VendorModelName" name="Vendor model name" />
        <subtest id="ModelName" name="Customer model name" />
        <subtest id="Serial" name="Serial number" />
        <subtest id="getScannedSerial" name="Scanned serial number value" />
        <subtest id="ScannedSerial" name="Scanned serial number" />
        <subtest id="FirmwareVersion" name="Software version" />
        <subtest id="IR_C" name="IR_C" customer="Rogers" />
        <subtest id="EchoDct" name="Echo_DCT" customer="Rogers" />
        <subtest id="FirmwareValidation" name="Firmware validation" />
</subtests>

I need to parse it to Java List. And i need to get the "id" and "name" options. So I tried to create a class like this, but have an error:

@XmlRootElement(name="subtests")
public class Subtests {

    private List<Subtest> subtests;

    public List<Subtest> getSubtests() {
        return subtests;
    }

    @XmlElement ( name = "subtest" )
    public void setSubtests(List<Subtest> subtests) {
        this.subtests = subtests;
    }

    @Override
    public String toString() {
        return "Subtests [subtests=" + subtests + "]";
    }

} 

Do how can i parse this file to get the attributes of this XML? I have got a Subtest class like this:

@XmlRootElement ( name = "subtest" )
public class Subtest {

    private String id;
    private String name;
    private String customer;

    @XmlAttribute ( name = "id", required = true )
    public String getId() {
        return id;
    }

    @XmlAttribute ( name = "name", required = true )
    public String getName() {
        return name;
    }

    @XmlAttribute ( name = "customer", required = true )
    public String getCustomer() {
        return customer;
    }


    public void setId(String id) {
        this.id = id;
    }


    public void setName(String name) {
        this.name = name;
    }


    public void setCustomer(String customer) {
        this.customer = customer;
    }

}

and I need to get the list of such objects from the XML

Upvotes: 1

Views: 2032

Answers (2)

aravinth
aravinth

Reputation: 426

Use DOM parser to get the attributes

import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;



public class GetAttributes {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<subtests>\n" +
                "        <subtest id=\"Detect\" name=\"Device detection\" />\n" +
                "        <subtest id=\"DeviceType\" name=\"Device type\" />\n" +
                "        <subtest id=\"VendorName\" name=\"Vendor\" />\n" +
                "        <subtest id=\"VendorModelName\" name=\"Vendor model name\" />\n" +
                "        <subtest id=\"ModelName\" name=\"Customer model name\" />\n" +
                "        <subtest id=\"Serial\" name=\"Serial number\" />\n" +
                "        <subtest id=\"getScannedSerial\" name=\"Scanned serial number value\" />\n" +
                "        <subtest id=\"ScannedSerial\" name=\"Scanned serial number\" />\n" +
                "        <subtest id=\"FirmwareVersion\" name=\"Software version\" />\n" +
                "        <subtest id=\"IR_C\" name=\"IR_C\" customer=\"Rogers\" />\n" +
                "        <subtest id=\"EchoDct\" name=\"Echo_DCT\" customer=\"Rogers\" />\n" +
                "        <subtest id=\"FirmwareValidation\" name=\"Firmware validation\" />\n" +
                "</subtests>";


        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
        org.w3c.dom.Document doc = db.parse(bis);
        NodeList nodeList = doc.getElementsByTagName("subtest");
        for(int x=0,size= nodeList.getLength(); x<size; x++) 
        {
            System.out.println(nodeList.item(x).getAttributes().getNamedItem("id").getNodeValue()+ " ========== "+ nodeList.item(x).getAttributes().getNamedItem("name").getNodeValue());
        }



    }
}

Upvotes: 2

Otis Ottington
Otis Ottington

Reputation: 477

If you want use JAXB, here is an example: JAXB Tutorial

In the last section the unmarshaller creates java objects from an existing xml structure

Upvotes: 0

Related Questions