2

I am trying to parse a XML string into org.w3c.dom.Document object.

I have looked at solutions provided here, here and a few other blogs that give a variation of the same solution. But the Document object's #Document variable is always null and nothing gets parsed.

Here is the XML file

 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 call v.getXML().toString() I get [#document: null]

Clearly, the parse is failing. But I don't understand why.

Any suggestions/help is greatly appreciated.

Community
  • 1
  • 1
anu
  • 1,017
  • 1
  • 19
  • 36
  • 1
    Clearly an exception is being thrown that you are ignoring. DocumentBuilder.parse() doesn't return null. You're also contradicting yourself. Either getXML() returns null, in which case 'this.xml' is null, or 'this.xml' isn't null and is inspectable as you describe. Not both at the same time. – user207421 Apr 15 '14 at 02:01
  • 1
    If the inspection says the document was read, the document was read. Are you sure you're calling getXML() against the object which ran the parse? Are you sure the value you're getting back is really null, as opposed to just having a toString() that returns null? – keshlam Apr 15 '14 at 02:36
  • you're right. I meant, getXML().toString() returns null. getXML() is not null. Any ideas why ? – anu Apr 15 '14 at 02:40
  • Why are you using Document.toString() at all? It isn't obliged to have any specific behaviour. Try use the Document interface correctly. – user207421 Apr 15 '14 at 04:15
  • I had recently used Nokogiri and I was confused as to why I was not able to getElementsByTag() from the document, which Nokogiri has... Nevertheless, I was able to use the Document object correctly. getChildNodes() method was crucial in doing what I needed to do. – anu Apr 15 '14 at 05:59
  • 1
    @Anupam put it as the answer then. Or do you only want to help yourself? – Kyle Bridenstine Dec 21 '14 at 03:15

0 Answers0