1

Following is my xml:

 <Body>
  <tag1 xmlns=""> <innerTag></innerTag> </tag1>
  </Body>

The problem is that I am not able to get the string inside <tag1></tag1>, that is <innerTag></innerTag>. Following is my logic:

public void startElement(final String uri, final String localName,
            final String qName, final Attributes attributes)
            throws SAXException {
        if ("tag1".equalsIgnoreCase(qName)){
            inTag1 = true;
            System.out.println("start");
        }
}


public void endElement(final String uri, final String localName,
            final String qName) throws SAXException {
        if ("tag1".equalsIgnoreCase(qName)) {
            System.out.println("end");
            inTag1 = false;
        }
}

public void characters(final char[] ch, final int start, final int length) {

        if (inTag1) {
            System.out.println("@@@" + new String(ch, start, length));
        }
    }
}

But it is giving me empty output. Can anyone help.

2
  • innerTag is an element not a string or a text node. If you want to print it, you will have to do it in the startElement and endElement callback. Commented Sep 23, 2012 at 10:03
  • The <innerTag> will be dealt with by startElement (and the closing tag by endElement, just print qname in the else-part of your condition). So the characters handler won't see them. Commented Sep 23, 2012 at 10:04

2 Answers 2

1

In your comment to UVM's answer you wrote

Actually inner tag is a kind of xml inside this xml. So I want that as a whole

There is no way to tell the SAX parser to not process all of the XML inside an element and return the entire contents as a string. Essentially, you have two options:

  • reconstruct the XML string, by listening out to all of the SAX events and building the XML string up yourself, or
  • if you're in control of the XML documents you're attempting to parse, changing the format of them to something like

    <Body>
      <tag1 xmlns=""><![CDATA[ <innerTag></innerTag> ]]></tag1>
    </Body>
    
Sign up to request clarification or add additional context in comments.

Comments

0

You need to check "innerTag" intead of "tag1"

if ("innerTag".equalsIgnoreCase(qName)){
            inTag1 = true;
            System.out.println("start");
        }

Basically your innerTag is a child element of tag1.So SAX parser keep on parsing because for it, it is till a valid XML element.

2 Comments

Actually inner tag is a kind of xml inside this xml. So I want that as a whole.
You will not get it like that because SAX parser in event based parsing.If at all if you want then, you will have to do it manually.In the above code, if parser sees this innertag , then you need to construct it xml string by yourself

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.