Skip to content

Instantly share code, notes, and snippets.

@sfalquier
Created September 3, 2012 08:43
Show Gist options
  • Save sfalquier/3607940 to your computer and use it in GitHub Desktop.
Save sfalquier/3607940 to your computer and use it in GitHub Desktop.
load W3C xml Document object from a String
public static org.w3c.dom.Document loadXMLFromString(String xml)
throws org.xml.sax.SAXException, java.io.IOException {
return loadXMLFrom(new java.io.ByteArrayInputStream(xml.getBytes()));
}
public static org.w3c.dom.Document loadXMLFrom(java.io.InputStream is)
throws org.xml.sax.SAXException, java.io.IOException {
javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory
.newInstance();
factory.setNamespaceAware(true);
javax.xml.parsers.DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (javax.xml.parsers.ParserConfigurationException ex) {
}
org.w3c.dom.Document doc = builder.parse(is);
is.close();
return doc;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment