File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes reading xml elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading xml elements" Watch "reading xml elements" New topic

reading xml elements

selenium Two

Joined: Aug 09, 2010
Posts: 1
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

Joined: Mar 20, 2007
Posts: 317
Welcome to JR! Before you get any answers on this forum, you might want to change your display name.
David Newton

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 02, 2003
Posts: 11956

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46424
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).
I agree. Here's the link:
subject: reading xml elements
It's not a secret anymore!