简体   繁体   English

xml中的文档生成器给出错误

[英]Document builder from xml gives error

I am trying to parse a html response which is in xml format. 我正在尝试解析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

However I get an error on line which I have commented on. 但是我在网上评论了一个错误。

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

I had the same problem. 我有同样的问题。
For me, problem was due to a bad import of class InputSource. 对我来说,问题是由于InputSource类的导入错误所致。

So, I had imported jdk.internal.org.xml.sax.InputSource instead of org.xml.sax.InputSource . 因此,我导入了jdk.internal.org.xml.sax.InputSource而不是org.xml.sax.InputSource With org.xml.sax.InputSource the problem is solved. 使用org.xml.sax.InputSource解决了该问题。

Modified code, Try this 修改后的代码,试试看

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM