I have the following code:
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
How can I get it to parse XML contained within a String instead of a file?
I have the following code:
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlFile);
How can I get it to parse XML contained within a String instead of a file?
One way is to use the version of parse that takes an InputSource rather than a file
A SAX InputSource can be constructed from a Reader object. One Reader object is the StringReader
So something like
Convert the string to an InputStream and pass it to DocumentBuilder
EDIT
In response to bendin's comment regarding encoding, see shsteimer's answer to this question.
I have this function in my code base, this should work for you.
also see this similar question
You can use the Scilca XML Progession package available at GitHub.
I'm using this method
javadocs show that the parse method is overloaded.
Create a StringStream or InputSource using your string XML and you should be set.