Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading xml elements

 
selenium Two
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

}
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JR! Before you get any answers on this forum, you might want to change your display name.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please see the JavaRanch naming policy and change your display name to conform with this policy. Thanks! And welcome to JavaRanch!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12185
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic