Skip to content

Instantly share code, notes, and snippets.

@AmanTifli
Created December 13, 2013 13:42
Show Gist options
  • Save AmanTifli/7944375 to your computer and use it in GitHub Desktop.
Save AmanTifli/7944375 to your computer and use it in GitHub Desktop.
Takes a string of xml and writes it to an xml file
public static void stringToDom(String xmlSource)
throws SAXException, ParserConfigurationException, IOException, TransformerException {
//parse the given input
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
//write the document
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("outFile.xml"));
transformer.transform(source, result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment