简体   繁体   中英

not able to parse tags using sax parser

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.

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> 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM