parse method in DocumentBuilder is returning an er

2019-09-09 20:01发布

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?

2条回答
萌系小妹纸
2楼-- · 2019-09-09 20:29

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

查看更多
等我变得足够好
3楼-- · 2019-09-09 20:41

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;

查看更多
登录 后发表回答