This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes XML and Related Technologies and the fly likes simple JAXPquestion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "simple JAXPquestion" Watch "simple JAXPquestion" New topic
Author

simple JAXPquestion

Marina Smith
Greenhorn

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

<?xml version="1.0" encoding="UTF-8"?>
<cp-datasource>
<name>name</name>
<password>password</password>
<url>url</url>
</cp-datasource>
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(file));
Node datasource = document.getDocumentElement();
NodeList l = datasource.getChildNodes();
System.out.println(l.getLength());
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(name.getData());
System.out.println(password.getData());
System.out.println(url.getData());
System.out.println(l.getLength()); - returns 7
I get null pointer

Please help..
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: simple JAXPquestion
 
Similar Threads
Key Search in XML File through DOM
parsing the XML tags values having the same name
xml problem
Updating XML values
parse siblings in DOM