来自xml的文档构建器给出了错误

时间:2014-03-27 10:28:33

标签: java html xml xml-parsing

我正在尝试解析xml格式的html响应。

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStream in = new FileInputStream(htmlresponse);
        InputSource inputSource = new InputSource(new InputStreamReader(in));
        Document xmlDocument = builder.parse(inputSource);   //Error is here

但是我在线上发现了一个错误,我已经评论过了。

Error - "the method parse(InputStream) in the type DocumentBuilder is not applicable for the arguments (InputSource)"

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题 对我来说,问题是由于类InputSource导入不好 所以,我导入了jdk.internal.org.xml.sax.InputSource而不是org.xml.sax.InputSource。使用org.xml.sax.InputSource可以解决问题。

答案 1 :(得分:0)

修改后的代码,试试这个

    String xml = "<test>this is a test xml</test>";
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document xmlDocument = builder.parse(new InputSource(new StringReader(xml)));