File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes simple JAXPquestion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "simple JAXPquestion" Watch "simple JAXPquestion" New topic

simple JAXPquestion

Marina Smith

Joined: Oct 03, 2003
Posts: 1
I am tring to parse a simple xml:

<?xml version="1.0" encoding="UTF-8"?>
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(file));
Node datasource = document.getDocumentElement();
NodeList l = datasource.getChildNodes();
Node n = l.item(0);
Node p = l.item(1);
Node u = l.item(2);
Text name = (Text) n.getFirstChild();
Text password = (Text) p.getFirstChild();
Text url = (Text) u.getFirstChild();
System.out.println(l.getLength()); - returns 7
I get null pointer

Please help..
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Marina, the problem is that the XML parser parses whitespace into #text nodes, which causes the NodeList to be of length 7. You can fix this by either removing all whitespace from the XML document (everything on a single line) or by adding the necessary logic into your tree-traversing Java code for ignoring all whitespace nodes.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
I agree. Here's the link:
subject: simple JAXPquestion
It's not a secret anymore!