Java XML String Transform writeToFile(String xmlContent, String path)

Here you can find the source of writeToFile(String xmlContent, String path)

Description

write To File

License

Apache License

Declaration

public static org.w3c.dom.Document writeToFile(String xmlContent, String path) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.InputSource;

public class Main {
    public static org.w3c.dom.Document writeToFile(String xmlContent, String path) {
        System.out.println("This is the path " + path);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;//from   w  w w.  j  a  va  2s . co m
        try {
            builder = factory.newDocumentBuilder();
            org.w3c.dom.Document doc = builder.parse(new InputSource(new StringReader(xmlContent)));

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(path));

            transformer.transform(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }
}

Related

  1. write(Node node, OutputStream out, String... props)
  2. writeElement(Element element, String fileName)
  3. writeNodeSubtreeXMLString(final Node node, final Writer writer)
  4. writePropToString(Properties props)
  5. writeResultToFile(final StreamResult result, final String path)
  6. writeXML(Node node, String dtdFilename, String outputFileName)
  7. writeXml(OutputStream os, Node node, String encoding)
  8. writeXmlFile(Element doc, String filename)
  9. writeXmlResult(String xml, Writer w)