I am wanting to convert a xml string to a xml file. I am getting a xml string as an out put and I have the following code so far:
public static void stringToDom(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
//return builder.parse(new InputSource(new StringReader(xmlSource)));
}
However Im not too sure where I go from here. I am not creating the file anywhere, so how do I incorporate that into it?
I am passing my xml string into xmlSource.
In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.
Write XML to a file Steps to create and write XML to a file. Create a Document doc . Create XML elements, attributes, etc., and append to the Document doc . Create a Transformer to write the Document doc to an OutputStream .
If you just want to put the content of a String in a file, it doesn't really matter whether it is actually XML or not. You can skip the parsing (which is a relatively expensive operation) and just dump the String
to file, like so:
public static void stringToDom(String xmlSource)
throws IOException {
java.io.FileWriter fw = new java.io.FileWriter("my-file.xml");
fw.write(xmlSource);
fw.close();
}
If you want to be on the safe side and circumvent encoding issues, as pointed by Joachim, you would need parsing however. Since its good practice to never trust your inputs, this might be the preferable way. It would look like this:
public static void stringToDom(String xmlSource)
throws SAXException, ParserConfigurationException, IOException {
// Parse the given input
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
// Write the parsed document to an xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("my-file.xml"));
transformer.transform(source, result);
}
public static void stringToDom(String xmlSource) throws SAXException, ParserConfigurationException, IOException, TransformerException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlSource)));
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("c:/temp/test.xml"));
transformer.transform(source, result);
}
Source : http://docs.oracle.com/javaee/1.4/tutorial/doc/JAXPXSLT4.html
If your XML string is clean and ready to be written, why don't you copy it into a file with .xml at the end ?
With Java 1.7 :
Path pathXMLFile = Paths.get("C:/TEMP/TOTO.XML");
Files.write(pathXMLFile, stringXML.getBytes(), StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE);
Easy and quick :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With