In Java, how do I parse XML as a String instead of a file?
I have this function in my code base, this should work for you.
public static Document loadXMLFromString(String xml) throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is);}
also see this similar question
Convert the string to an InputStream and pass it to DocumentBuilder
final InputStream stream = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();builder.parse(stream);
EDIT
In response to bendin's comment regarding encoding, see shsteimer's answer to this question.