1

Is it possible to give path expressions in SAX parser? I have an XML file which has a few same name tags, but they are in different element. Is there any way to differentiate between them. Here is the XML:

<report id="322077">
        <update>
          <when>1136117311</when>
          <what>nobody@mozilla.org</what>
        </update>
        <update>
          <when>1136117422</when>
          <what>asqueella@gmail.com</what>
        </update>
      </report>
    <report id="322074">
        <update>
          <when>1136117311</when>
          <what>nobody@mozilla.org</what>
        </update>
        <update>
          <when>1136117422</when>
          <what>asqueella@gmail.com</what>
        </update>
      </report>
    <report id="322076">
        <update>
          <when>1136117311</when>
          <what>nobody@mozilla.org</what>
        </update>
        <update>
          <when>1136117422</when>
          <what>asqueella@gmail.com</what>
        </update>
      </report>
shekhar pande
  • 1,172
  • 1
  • 11
  • 21
  • Please refer http://stackoverflow.com/questions/1863250/is-there-any-xpath-processor-for-sax-model – rpfun12 Mar 07 '17 at 02:29
  • Is it mandatory that you use a SAX parser? Navigating the structure is a lot easier if you read it into a DOM first. You then have lots of utility methods available to search and process nodes. – sprinter Mar 07 '17 at 02:36
  • not mandatory that you use a SAX parser. how can i do it with dom – shekhar pande Mar 07 '17 at 02:56
  • SAX is a very low-level parsing interface, if you want to use it for its performance benefits then you are expected to track your position in the document "by hand". What are you actually trying to do with this file? – Michael Kay Mar 07 '17 at 12:05

1 Answers1

0
String xml= // your xml
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
String expression="//update"; // See XPath
XPathExpression expr = xpath.compile(expression) ; 
    NodeList nodes  = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int k = 0; k < nodes.getLength(); k++) {
// Do what you want with that