2

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")) {
             //??????????????????

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.. I ended up using JDOM.
1

Check this

check the method parser.getElementText()

HTH

This would get you the inner text, you may add the starting and ending name manually by a normal method. And since XML is a strict standard you're fine.

1 Comment

not really what I am looking for, my example xml is simple, but in reality it is complex and contains annotations to boot. Plus the parser.getElementText() threw an exception for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.