I am trying to parse a XML string into `org.w3c.dom.Document` object.
I have looked at solutions provided in a few blogs that give a variation of the same solution that I have written. But the `Document` object's #Document variable is always null and nothing gets parsed.
Here is the XML
XMLMappingValidator v = new XMLMappingValidator("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<mapping>\n" + "<container>\n" + "<source-container>c:\\stem.csv</source-container>\n" + "<source-type>CSV_FILE</source-type>\n" + "<target-container>db</target-container>\n" + "<target-type>MySQL_TABLE</target-type>\n" + "</container>\n" + "<entity>\n" + "<source-entity>stem</source-entity>\n" + "<target-entity>tbl_stem</target-entity>\n" + "</entity>\n" + "<attributes>\n" + "<attribute>\n" + "<datatype>SMALLINT</datatype>\n" + "<source-attribute>id</source-attribute>\n" + "<target-attribute>id</target-attribute>\n" + "</attribute>\n" + "<attribute>\n" + "<datatype>VARCHAR</datatype>\n" + "<source-attribute>name</source-attribute>\n" + "<target-attribute>name</target-attribute>\n" + "</attribute>\n" + "<attribute>\n" + "<datatype>TEXT</datatype>\n" + "<source-attribute>body</source-attribute>\n" + "<target-attribute>body</target-attribute>\n" + "</attribute>\n" + "<attribute>\n" + "<datatype>DATETIME</datatype>\n" + "<source-attribute>date</source-attribute>\n" + "<target-attribute>date</target-attribute>\n" + "</attribute>\n" + "<attribute>\n" + "<datatype>VARCHAR</datatype>\n" + "<source-attribute>info</source-attribute>\n" + "<target-attribute>info</target-attribute>\n" + "</attribute>\n" + "</attributes>\n" + "</mapping>");
The constructor XMLMappingValidator object's code is as follows
public class XMLMappingValidator { private Document xml; public XMLMappingValidator (String xmlString) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); this.xml = builder.parse(new InputSource(new StringReader(xmlString))); } public Document getXML() { return xml; } }
When I callI get**v.getXML().toString()**`[#document: null]`
Clearly, the parse is failing. But I don't understand why.
Any suggestions/help is greatly appreciated.
Thanks