解析具有多个要列出的属性的XML

时间:2016-07-20 12:35:47

标签: java xml jaxb

我有一个像这样的XMl文件:

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

我需要将它解析为Java List。而且我需要获得&#34; id&#34;和&#34;名称&#34;选项。所以我尝试创建一个这样的类,但有一个错误:

@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 + "]";
    }

} 

我该如何解析此文件以获取此XML的属性?我有一个像这样的Subtest类:

@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;
    }

}

我需要从XML

获取此类对象的列表

2 个答案:

答案 0 :(得分:1)

使用DOM解析器获取属性

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



    }
}

答案 1 :(得分:0)

如果你想使用JAXB,这是一个例子: JAXB Tutorial

在上一节中,unmarshaller从现有的xml结构创建java对象