Project

Profile

Help

How do I iterate through a NodeInfo/XPath

Added by Anonymous over 18 years ago

Legacy ID: #3356860 Legacy Poster: Ulricoher (ulricoher)

Hi everyone, I would be very thankful if you could help me. I have following xml-File: <?xml version="1.0" encoding="UTF-8"?> <main xmlns="com.dvb.app.ProductionOrder" xsi:schemaLocation="com.cisag.app.production.obj"> <ProductionOrder xmlns="com.dvb.app.ProductionOrder"> <Details> <number>110</number> </Details> <Details> <number>120</number> </Details> </ProductionOrder> </main> In my java Code, I want to access the Details to receive the numbers: XPathFactory xpf = XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL); XPath xpe = xpf.newXPath(); StandaloneContext context = new StandaloneContext(); context.declareNamespace("a","com.dvb.app.ProductionOrder"); NamespaceContext namespaceContext = new NamespaceContextImpl(context); xpe.setNamespaceContext(namespaceContext); XPathExpression xpathExpression = xpe.compile("/a:main"); XPathExpression findNumber = xpe.compile("string(ancestor::Details/number)"); InputSource is = new InputSource(new File("test.xml"); SAXSource ss = new SAXSource(is); NodeInfo doc = ((XPathEvaluator)xpe).setSource(ss); List matchedLines = (List)xpathExpression.evaluate(doc, XPathConstants.NODESET); for (Iterator iter = matchedLines.iterator(); iter.hasNext();) { NodeInfo line = (NodeInfo)iter.next(); //Comment: What can I do here to access the number of the details //If I try // xpathExpression findProductionOrder = // xpe.compile("/a:ProductionOrder") and // List prdOrList = (List) findProductionOrder.evaluate(line, XPathConstants.NODESET) // no production order is found } Any Idea? Thanks a lot in advance & regards, Ulrich


Replies (2)

RE: How do I iterate through a NodeInfo/XPath - Added by Anonymous over 18 years ago

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

Your first expression finds the "main" element. To find all the "number" elements starting from this as the context node you need the XPath expression a:ProductionOrder/a:Details/a:number I'm not sure what your second XPath expression is designed to do: it's looking for an ancestor called Details, so it's only going to be useful if called with a descendant of Details as the context node. You might find it easier to test these XPath expressions from an XSLT environment first, even if you then want to deploy them from Java.

RE: How do I iterate through a NodeInfo/XPath - Added by Anonymous over 18 years ago

Legacy ID: #3358137 Legacy Poster: Ulricoher (ulricoher)

Hi Michael, your expression a:ProductionOrder/a:Details/a:number is the right expression for my purposes: XPathExpression findProductionOrder = xpe.compile("//a:ProductionOrder/a:Details/a:number"); .... for (Iterator iter = matchedLines.iterator(); iter.hasNext();) { NodeInfo line = (NodeInfo)iter.next(); List matchingProductionOrders = (List)findProductionOrder.evaluate(line, XPathConstants.NODESET); for (Iterator iterPO = matchingProductionOrders.iterator(); iterPO.hasNext();) { NodeInfo matchingProductionOrder = (NodeInfo) iterPO.next(); System.out.println( matchingProductionOrder.getStringValue()); } } The printed output is 110 and 120. Now I'm really happy, thanks a lot, Ulrich

    (1-2/2)

    Please register to reply