Why DocumentBuilder.parse () is not working

I read a few posts on how to use the DocumentBuilder.parse() function to get the document object.

 Document document = builder.parse(new InputSource(new StringReader(xml))); 

it returned [#document: null] , which, as I found, does not necessarily mean that it is empty. However, having examined it more, I found that it is actually empty.

I build xml lines and used the xml validator, (and pasted into eclipse and ctrl + shift + f to format it. This is usually my first attempt to see that something is well formed) to show that it is really xml. I decided to break down each part of the parse() parameters so that I could go through and see that they worked correctly.

My code is:

 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBuilder(); StringReader sr = new StringReader(xml); InputSource is = new InputSource(sr); Document document = builder.parse(is); return document; } catch(Exception e){ e.printStackTrace(); } 

sr and seems to work correctly until I ran the line builder.parse (is). Once this is done, the value of sr.str becomes zero and the same with is.characterInputStream.str. It seems strange to me, does it? It drove me crazy, any input would be wonderful!

edit- my xml line:

 <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> <channel> <title>Feed Title</title> <link>Feed Link</link> <description>Feed Description</description> <item> <title>Item Title</title> <link>Item Link</link> <description>Item Description</description> </item> <item> <title>Another Item</title> <link>Another Link</link> <description>Another Description</description> </item> </channel> </rss> 
+6
source share
1 answer

Once this is done, the value of sr.str becomes zero and the same with is.characterInputStream.str. It seems strange to me, was it expected?

Yes, I would say that. DocumentBuilder.parse closes the reader. StringReader.close() sets str to null . This is a StringReader implementation StringReader - but you should expect to see implementation details when you push private fields when debugging. (He also did not document that DocumentBuilder.parse will close his input, but it seems reasonable.)

It's unclear what the problem is with your XML, but this part of the behavior is perfectly reasonable.

I highly recommend that you try your code using the simplest XML you can think of, for example. "<foo />" .

The code you have shown so far is ok. Here's a short but complete program showing that it works:

 import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.*; import java.io.*; class Test { public static void main(String [] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; builder = factory.newDocumentBuilder(); StringReader sr = new StringReader("<foo />"); InputSource is = new InputSource(sr); Document document = builder.parse(is); System.out.println(document.getDocumentElement().getTagName()); } } 
+8
source

All Articles