I'm trying to parse a string that contains an XML, into a Document Object in Java. Here's the source code of the method:
private Document getDocument(String xml) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
doc = builder.parse(new InputSource(new StringReader(xml)));
} catch (SAXException e) {
System.out.println(e.toString());
} catch (IOException e) {
System.out.println(e.toString());
}
return doc;
}
What I always get as a value of the doc variable according to the debugger is
[#document: null]
Does anyone have a clue what is wrong?
Thank you for your time!