Search code examples
javaxmlstax

Java Stax how to get only value of specific child nodes


I use Stax for get nodeName and nodeValue of my xml file (size 90 MB) :

<?xml version="1.0" encoding="UTF-8"?>
<name1>
    <type>
        <coord>67</coord>
        <umc>57657</umc>
    </type>
    <lang>
        <eng>989</eng>
        <spa>123</spa>
    </lang>
</name1>
<name2>
    <type>
        <coord>534</coord>
        <umc>654654</umc>
    </type>
    <lang>
        <eng>354</eng>
        <spa>2424</spa>
    </lang>
</name2>
<name3>
    <type>
        <coord>23432</coord>
        <umc>14324</umc>
    </type>
    <lang>
        <eng>141</eng>
        <spa>142</spa>
    </lang>
</name3>

I can get localName but not child nodes... if I want to get the value for all child nodes different of 'spa' how can I process to get that ?

Java:

XMLStreamReader dataXML = factory.createXMLStreamReader(new FileReader(path));
while (dataXML.hasNext())
{
    int type = dataXML.next();
    switch(type)
    {
        case XMLStreamReader.START_ELEMENT:
             System.out.println(dataXML.getLocalName());
             break;

        case XMLStreamReader.CHARACTERS:
             System.out.println(dataXML.getText());
             break;
     }
}

Solution

  • To keep track of element being parsed it's needed to introduce variable holding the current tag name as well as the variable with the tag name(s) of interest:

       String localname = null;
       String tagName = "spa";
    
        while (dataXML.hasNext()) {
            int type = dataXML.next();
            switch (type) {
    
                case XMLStreamReader.SPACE:
                    continue;
    
                case XMLStreamReader.START_ELEMENT:
                    localname = dataXML.getLocalName();
                    System.out.println(dataXML.getLocalName());
                    break;
    
                case XMLStreamReader.CHARACTERS:
                    if (!tagName.equals(localname)) {
                        System.out.println(dataXML.getText());
                    }
                    break;
            }
        }
    

    In case there are several tags you want to handle, variable tagName could be replaced with a list:

    List<String> tagNames = new ArrayList<>();
    tagNames.add("spa");
    

    And the check would be following:

    if (!tagNames.contains(localname)) {
        System.out.println(dataXML.getText());
    }