1

I'm trying to parse xml, downloaded from the web, in java, following examples from here (stackoverflow) and other sources.

First I pack the xml in a string:

String xml = getXML(url, logger);

If I printout the xml string at this point:

System.out.println("XML " + xml);

I get a printout of the xml so I'm assuming there is no fault up to this point. Then I try to create a document that I can evaluate:

InputSource is= new InputSource(new StringReader(xml));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(is);

If I print out the document here:

System.out.println("Doc: " + doc);

I get: Doc: [#document: null]

When I later try to evaluate expressions with Xpath I get java.lang.NullPointerException and also when just trying to get the length of the root:

System.out.println("Root length " + rootNode.getLength());

which leaves me to believe the document (and later the node) is truly null.

When I try to print out the Input Source or the Node I get eg.

Input Source: org.xml.sax.InputSource@29453f44

which I don't know how to interpret.

Can any one see what I've done wrong or suggest a way forward? Thanks in advance.

MarkD
  • 11
  • 3
  • Possible duplicate of [DocumentBuilder.parse(InputStream) returns null](http://stackoverflow.com/questions/2018868/documentbuilder-parseinputstream-returns-null) – Jordi Castilla Oct 26 '15 at 13:01

1 Answers1

0

You may need another way to render the document as a string.

For JDOM:

 public static String toString(final Document document) {
   try {
     final ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

     final XMLOutputter outp = new XMLOutputter();
      outp.output(document, out);
     final String string = out.toString("UTF-8");
     return string;
   }
   catch (final Exception e) {
     throw new IllegalStateException("Cannot stringify document.", e);
   }
 }

The output

org.xml.sax.InputSource@29453f44

simply is the class name + the hash code of the instance (as defined in the Object class). It indicates that the class of the instance has toString not overridden.

Robert Reiner
  • 531
  • 3
  • 9
  • Thanks for your suggestion. I don't it's the conversion to string that is problem, when I later try to evaluate expressions with Xpath on the document I get a "Null node" exception but I can try it out if you tell me where XMLOutputter() comes from? – MarkD Oct 26 '15 at 13:30
  • This applies only if you use JDOM (other libraries may have similar classes): http://www.jdom.org/docs/apidocs/org/jdom2/output/XMLOutputter.html – Robert Reiner Oct 26 '15 at 13:38