0

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?

Wilson
  • 11,339
  • 2
  • 29
  • 33
Armando Xhimanki
  • 115
  • 1
  • 11

2 Answers2

3

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

Wilson
  • 11,339
  • 2
  • 29
  • 33
1

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;

Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34