dynamically read xml element and value in java

人走茶凉 提交于 2019-12-04 21:30:56
public static void main(String[] args) throws SAXException, IOException,
        ParserConfigurationException, TransformerException {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
            .newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("document.xml"));

    NodeList nodeList = document.getElementsByTagName("*");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            // do something with the current element
            System.out.println(node.getNodeName());
        }
    }
}

i think this will help

You could use a StAX parser for this purpose.

XML sample employees.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employees count="4">
    <employee>
        <id>2</id>
        <firstName>Jane</firstName>
        <lastName>Doe</lastName>
        <income>20000.0</income>
    </employee>
    <employee>
        <id>3</id>
        <firstName>Alfred</firstName>
        <lastName>Pennyworth</lastName>
        <income>30000.0</income>
    </employee>
    <employee>
        <id>4</id>
        <firstName>Tony</firstName>
        <lastName>Stark</lastName>
        <income>40000.0</income>
    </employee>
    <employee>
        <id>42</id>
        <firstName>Foo</firstName>
        <lastName>Bar</lastName>
        <income>15.0</income>
    </employee>
</employees>

Java code

try (InputStream stream = new FileInputStream("employees.xml")) {
    XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);

    XMLStreamReader reader = inputFactory.createXMLStreamReader(stream);

    while (reader.hasNext()) {
        switch (reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                System.out.println("Start " + reader.getName());
                for (int i = 0, count = reader.getAttributeCount(); i < count; i++) {
                    System.out.println(reader.getAttributeName(i) + "=" + reader.getAttributeValue(i));
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                System.out.println("End " + reader.getName());
                break;
            case XMLStreamConstants.CHARACTERS:
            case XMLStreamConstants.SPACE:
                String text = reader.getText();
                if (!text.trim().isEmpty()) {
                    System.out.println("text: " + text);
                }
                break;
        }
    }
}

Note that you could easily modify the START_ELEMENT case to compare the tag name to some value. (Probably you could ignore any other cases if you only want to check those elements.)

Output

Start employees
count=4
Start employee
Start id
text: 2
End id
Start firstName
text: Jane
End firstName
Start lastName
text: Doe
End lastName
Start income
text: 20000.0
End income
End employee
Start employee
Start id
text: 3
End id
Start firstName
text: Alfred
End firstName
Start lastName
text: Pennyworth
End lastName
Start income
text: 30000.0
End income
End employee
Start employee
Start id
text: 4
End id
Start firstName
text: Tony
End firstName
Start lastName
text: Stark
End lastName
Start income
text: 40000.0
End income
End employee
Start employee
Start id
text: 42
End id
Start firstName
text: Foo
End firstName
Start lastName
text: Bar
End lastName
Start income
text: 15.0
End income
End employee
End employees
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!