Skip to content

Instantly share code, notes, and snippets.

@packmad
Created December 28, 2015 15:20
Show Gist options
  • Save packmad/fbe39ddd44b1c4b4f31d to your computer and use it in GitHub Desktop.
Save packmad/fbe39ddd44b1c4b4f31d to your computer and use it in GitHub Desktop.
transform a String into an org.w3c.dom.Document
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.xml.sax.InputSource;
public static Document loadXMLFromString(String xml) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
return documentBuilder.parse(inputSource);
} catch (ParserConfigurationException|SAXException|IOException e) {
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment