• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

simple JAXPquestion

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic