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?
The problem may be due to a bad import of class
InputSource
. Pleaseorg.xml.sax.InputSource
is imported .DocumentBuilder
acceptorg.xml.sax.InputSource
but notjdk.internal.org.xml.sax.InputSource
Use this import
import org.xml.sax.InputSource;
instead of thisimport jdk.internal.org.xml.sax.InputSource;
after doing this step then you will face error on Document modify that import fromimport javax.swing.text.Document;
toimport org.w3c.dom.Document;