• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading xml elements

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

}
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JR! Before you get any answers on this forum, you might want to change your display name.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic