2014-03-27 58 views
0

我正在嘗試解析xml格式的html響應。xml文檔生成器給出錯誤

 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

修改後的代碼,試試這個

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

「DocumentBuilder類型中的方法解析(InputStream)不適用於參數(InputSource)」 – Harshit

+0

方法parse()可以將InputSource或InputStream作爲Java7中的參數http://docs.oracle.com /javase/7/docs/api/javax/xml/parsers/DocumentBuilder.html。您使用的是哪個版本的Java? – Jay

+0

1.6.0_27。同時在eclipse中,當我指出可能的解決方案的錯誤時,它說「重命名文件」 – Harshit

2

我有同樣的問題。
對我來說,問題是由於類InputSource的錯誤導入導致的。
因此,我導入了jdk.internal.org.xml.sax.InputSource而不是org.xml.sax.InputSource。使用org.xml.sax.InputSource,問題就解決了。