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>
source share