parse method in DocumentBuilder is returning an er

2019-09-09 19:54发布

问题:

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;