我正在尝试解析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)"
答案 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)));