Search code examples
javaxmlstax

java stax get string of inner node


I am using java stax XMLStreamReader to read an xml. I want to grab the whole string of certain inner nodes.

XML:

<example>
   <ignoreMe>
      <bla></bla>
   </ignoreMe>
   <getMe>
      <data></data>
   </getMe>
</example>

I just want to be able to get the whole internal getMe node in a String. IE:

   <getMe>
      <data></data>
   </getMe>

Here is what I have.. but I am stuck:

XMLStreamReader parser = factory.createXMLStreamReader(new FileReader(file)); 
for (int event = parser.next();  
event != XMLStreamConstants.END_DOCUMENT;
event = parser.next()) {

    switch (event) {
    case XMLStreamConstants.START_ELEMENT:
        if (parser.getLocalName().equals("GetMe")) {
             //??????????????????

Solution

  • I started learning StaX last week, but if you are still looking for an answer to your problem, this may help you :

        XMLInputFactory xmlif = XMLInputFactory.newInstance();
        xmlif.setProperty(XMLInputFactory.IS_COALESCING, true);
        XMLStreamReader xmlsr;
        String resultat="";
        boolean isGetme=false;
    
        try {
            xmlsr = xmlif.createXMLStreamReader(new FileReader("lib/toto.xml"));
            int eventType;
            while (xmlsr.hasNext()) {
                eventType = xmlsr.next();
                switch (eventType) {
                    case XMLStreamConstants.START_ELEMENT:
                        if(xmlsr.getLocalName().equals("getMe")){
                            isGetme=true;
                        }
                        if(isGetme){
                            resultat+="<"+xmlsr.getLocalName()+">";
                        }
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        if(isGetme){
                            resultat+=xmlsr.getText();
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        if(xmlsr.getLocalName().equals("getMe")){
                            resultat+="</"+xmlsr.getLocalName()+">";
                            isGetme=false;
                        }
                        if(isGetme && !xmlsr.getLocalName().equals("getMe")){
                            resultat+="</"+xmlsr.getLocalName()+">";
                        }
                        break;
                    default:
                        break;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
        System.out.println(resultat);
    

    This code will get you all the inner nodes and text contents, and store it into a String. I.E:

    <getMe>
          <data>test</data>
    </getMe>
    

    However, this code is quite "ugly" and is surely not the best solution for what you are looking for, JDOM may be more suited to your needs.