How to convert an XML string to an XML file?

I want to convert an xml string to an xml file. I get the xml string as out put, and I have the following code:

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, I am not too sure where I am going from here. I do not create a file anywhere, so how do I include it in it?

I am passing my XML string to xmlSource.

+4
source share
4 answers

If you just want to put the contents of a string in a file, it really doesn't matter if it is actually XML or not. You can skip parsing (which is a relatively expensive operation) and just dump String to a file, for example:

 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 safe and get around encoding problems, as Joachim pointed out, you'll need a parsing. Since its good practice to never trust your source data, this may be the preferred way. It will 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); } 
+20
source
 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

+1
source

Just copy the contents of the XML string to another file with a .xml extension. You can use java.io for the same.

0
source

If your XML string is clean and ready to be written, why don't you copy it to a .xml file 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 fast :)

0
source

Source: https://habr.com/ru/post/1493399/


All Articles