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.

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
This applies only if you use JDOM (other libraries may have similar classes): jdom.org/docs/apidocs/org/jdom2/output/XMLOutputter.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.