Java将XML的InputStream转换为Document对象总是导致NULL

时间:2014-04-15 01:47:20

标签: java xml parsing

我正在尝试将XML字符串解析为org.w3c.dom.Document对象。

我查看了提供的解决方案herehere以及一些其他博客,这些博客提供了相同解决方案的变体。但Document对象的#Document变量始终为null,并且不会解析任何内容。

这是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>");

构造函数XMLMappingValidator对象的代码如下

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;
    }
}

当我调用 v.getXML()。toString()时,我得到[#document: null]

显然,解析失败了。但我不明白为什么。

非常感谢任何建议/帮助。

0 个答案:

没有答案