parse method in DocumentBuilder is returning an error

拈花ヽ惹草 提交于 2019-12-25 06:25:27

问题


I am trying to use the parse method that takes an InputSource to parse a DocumentBuilder instance.

The error is:

The method parse(InputStream) in the type DocumentBuilder is not applicable for the arguments (InputSource)

This is the code:

public static Document loadXMLFromString(String xml) throws Exception {
    DocumentBuilder factory = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = factory.parse(new InputSource(new StringReader(xml)));   
}

It is asking me to change to InputStream.. Checked the documentation and one of the parse methods in DocumentBuilder takes an InputSource. What is the problem?


回答1:


The problem may be due to a bad import of class InputSource. Please org.xml.sax.InputSource is imported .

DocumentBuilder accept org.xml.sax.InputSource but not jdk.internal.org.xml.sax.InputSource




回答2:


Use this import import org.xml.sax.InputSource; instead of this import jdk.internal.org.xml.sax.InputSource; after doing this step then you will face error on Document modify that import from import javax.swing.text.Document; to import org.w3c.dom.Document;



来源:https://stackoverflow.com/questions/38137291/parse-method-in-documentbuilder-is-returning-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!