Project

Profile

Help

Namespace Iterator throughts NULL ptr excep.

Added by Anonymous over 18 years ago

Legacy ID: #3419955 Legacy Poster: Anurag Chakravarti (anurag2002)

Hi Michael, I want to find the namespace bindings that are in scope for a particular element. In the below example (test2.xml) say I want to find out all the namespace bindings in the scope of element foo. ?xml version="1.0" encoding="utf-8"?> <foo xmlns="urn:test:default-namespace" xmlns:test="test"> <ns1:bar xmlns:ns1="urn:test:namespace1-uri" xmlns="urn:test:namespace1-uri"> <baz/> <ns2:baz xmlns:ns2="urn:test:namespace2-uri"/> </ns1:bar> <ns3:hi xmlns:ns3="urn:test:namespace3-uri"> <there/> </ns3:hi> </foo> The following code throws null ptr exception. I XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON); XPath xpe = xpf.newXPath(); InputSource is = new InputSource((InputStream)new FileInputStream("test2.xml")); SAXSource ss = new SAXSource(is); NodeInfo nodeInfo = ((XPathEvaluator)xpe).setSource(ss); StandaloneContext context = new StandaloneContext(); context.declareNamespace("a","urn:test:default-namespace"); NamespaceContext namespaceContext = new NamespaceContextImpl(context); xpe.setNamespaceContext(namespaceContext); XPathExpression findLine = xpe.compile("a:foo"); List matchedLines = (List)findLine.evaluate(nodeInfo, XPathConstants.NODESET); if(matchedLines != null) { for (Iterator iter = matchedLines.iterator(); iter.hasNext();) { // Get the next matching line NodeInfo line = (NodeInfo)iter.next(); System.out.println("Saxon xpath output: " + line.getDisplayName()); AxisIterator axisIterator = line.iterateAxis(Axis.NAMESPACE); if(axisIterator != null) { Item namespaceItem = axisIterator.current(); System.out.println("111"); System.out.println("Hey: " + namespaceItem.getStringValue()); System.out.println("222"); } else { System.out.println("nsAxisIterator null"); } }; // for }; // if Ouput is: Saxon xpath output: foo 111 Exception in thread "main" java.lang.NullPointerException at TestNS.main(TestNS.java:142) The output should have been: xmlns:test="test" xmlns="urn:test:default-namespace" Thanks for the help. -Anurag


Replies (1)

RE: Namespace Iterator throughts NULL ptr exc - Added by Anonymous over 18 years ago

Legacy ID: #3420159 Legacy Poster: Michael Kay (mhkay)

iterateAxis() returns an AxisIterator, which is a subclass of SequenceIterator. If you call current() on a SequenceIterator before calling next(), the value is null; you are getting an NPE because you are using the value of current(). Call next() to get the first node on the axis.

    (1-1/1)

    Please register to reply