0

I am learning how to use SAX with xpath from this tutorial. and I wrote the below code, but it returns null instead of displaying '30'

please let me know how to solve it, and is there any other library for using SAX with Xpath?

Code:

DefaultNamespaceContext nsContext = new DefaultNamespaceContext(); // an implementation of javax.xml.namespace.NamespaceContext
    nsContext.declarePrefix("xsd", Namespaces.URI_XSD);

    XMLDog dog = new XMLDog(nsContext);
    XPathResults results = dog.sniff(new InputSource("c:\\brem.xml"));

    Expression xpath1 = dog.addXPath("//node[@lat='53.0334062'] [@lon='8.8461545']/following-sibling::tag[1]/@v");
    Log.d("", "", ""+results.getResult(xpath1));

xml:

<?xml version='1.0' encoding='utf-8' ?>
<osm>
<node id="25779111" lat="53.0334062" lon="8.8461545"/>
<node id="25779112" lat="53.0338904" lon="8.846314"/>
<node id="25779119" lat="53.0337395" lon="8.8489255"/>
<tag k="maxspeed" v="30"/>
<tag k="maxspeed:zone" v="yes"/>
<node id="25779114" lat="53.334062" lon="8.841545"/>
<node id="25779117" lat="53.038904" lon="8.84614"/>
<node id="25779110" lat="53.033795" lon="8.489255"/>
<tag k="maxspeed" v="32"/>
<tag k="maxspeed:zone" v="no"/>
</osm>
Santhosh Kumar Tekuri
  • 3,012
  • 22
  • 22
rmaik
  • 1,076
  • 3
  • 15
  • 48

2 Answers2

1

You are doing it in the wrong order. Add xpath expressions before sniffing:

    XMLDog dog = new XMLDog(nsContext);
    Expression xpath1 = dog.addXPath("//node[@lat='53.0334062'] [@lon='8.8461545']/following-sibling::tag[1]/@v");
    XPathResults results = dog.sniff(new InputSource(...));

It's the sniffing that evaluates the expressions, and there were none in your case.

forty-two
  • 12,204
  • 2
  • 26
  • 36
1

You should add xpaths before sniffing:

DefaultNamespaceContext nsContext = new DefaultNamespaceContext();

XMLDog dog = new XMLDog(nsContext);
Expression xpath1 = dog.addXPath("//node[@lat='53.0334062'] [@lon='8.8461545']/following-sibling::tag[1]/@v");
XPathResults results = dog.sniff(new InputSource(""c:\\brem.xml"));
List<NodeItem> list = (List<NodeItem>)results.getResult(xpath1);
System.out.println(list.isEmpty() ? null : list.get(0).value);

this prints:

30
Santhosh Kumar Tekuri
  • 3,012
  • 22
  • 22
  • kindly please can u tell me why do we use "xsd" in the following line nsContext.declarePrefix("xsd", Namespaces.URI_XSD);? – rmaik Jul 31 '15 at 08:54
  • 1
    the "xsd" prefix is not required as it is not used in the xpath being evaluated. I updated the answer to reflect the same. thanks – Santhosh Kumar Tekuri Jul 31 '15 at 10:02
  • thanks, this could be a quest. for u as well if like to answer. http://stackoverflow.com/questions/31743402/accessing-existing-attribute-returns-null – rmaik Jul 31 '15 at 10:06
  • kindly, i tried to evalute this expression:Expression xpath1 = dog.addXPath("//node[@lat='53.071932'] [@lon='8.7840591']/following-sibling::tag[1]/@v"); on a huge xml file using xmlDog..but still it takes too much time, and as xmlDog uses sax, i expected the output to be really fast..any advice.. – rmaik Jul 31 '15 at 12:29
  • please raise an issue on github with sample xml – Santhosh Kumar Tekuri Jul 31 '15 at 13:32