Reputation: 2445
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.
Upvotes: 10
Views: 76058
Reputation: 9705
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);
}
Upvotes: 32
Reputation: 472
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
Upvotes: 2
Reputation: 448
Just copy the contents of XML string to another file with extension .xml . You can use java.io for the same .
Upvotes: -1
Reputation: 495
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 :)
Upvotes: 1