Forums Register Login

reading xml elements

+Pie Number of slices to send: Send
Here's the snipet. Every time it goes into the loop the elements are read but not added and there value is always null

public static List<Element> getChildrenByTagName() throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {


DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse("Input.xml");

XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
xpath.setNamespaceContext(new wdXMLNamespaceResolver(doc, false));
//XPathExpression xPathQuery = xpath.compile(query);

Node links = (Node)xpath.evaluate("/userdata/David/tester[@id='1000']", doc, XPathConstants.NODE);

List<Element> nodeList = new ArrayList<Element>();
for (Node child = links.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child.getNodeType() == Node.ELEMENT_NODE ) {
nodeList.add((Element) child);


}


Please help .. In a new role


}
// System.out.println(nodeList.toString()); //Testing
return nodeList;

}
+Pie Number of slices to send: Send
Welcome to JR! Before you get any answers on this forum, you might want to change your display name.
+Pie Number of slices to send: Send
Please see the JavaRanch naming policy and change your display name to conform with this policy. Thanks! And welcome to JavaRanch!
+Pie Number of slices to send: Send
i'd also suggest you learn to use the 'code' tags. When pasting source, just click the little "code" button and the tags will auto-populate. then paste your source code between them. It preserves the indentation and makes reading what you have MUCH easier.

I'd add them to your post, but you don't have proper indentation anyway, so it won't matter.
+Pie Number of slices to send: Send
How do you know it's null? Where does the null appear? You can't tell that from your code (which is difficult to read without the code tags; please use the "edit" button and add code tags).
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 969 times.
Similar Threads
Efficient XML Parsing
Wrong Document Error when adding deep copy node to new document (Resolved)
Retrieve CDTA content from XML file
Deleting the children from node - xml
Trouble selecting nodes between two siblings
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:58:00.