Java XML Document to File writeDoc(Document doc, File file)

Here you can find the source of writeDoc(Document doc, File file)

Description

write Doc

License

Open Source License

Declaration

public static void writeDoc(Document doc, File file) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *                       I N T E L   C O R P O R A T I O N
 *   //  w  w w. ja  v a2 s.c o  m
 *  Functional Group: Fabric Viewer Application
 *
 *  File Name: XMLUtils.java
 *
 *  Archive Source: $Source$
 *
 *  Archive Log:    $Log$
 *  Archive Log:    Revision 1.3  2015/08/17 18:49:02  jijunwan
 *  Archive Log:    PR 129983 - Need to change file header's copyright text to BSD license txt
 *  Archive Log:    - change backend files' headers
 *  Archive Log:
 *  Archive Log:    Revision 1.2  2015/03/13 14:29:17  jijunwan
 *  Archive Log:    improved to have consistent xml format when we add, remove or replace an Application, Device Group, or Virtual Fabric node.
 *  Archive Log:    improved to add comments to indicate the change was made by FM GUI and also the time we change it
 *  Archive Log:
 *  Archive Log:    Revision 1.1  2015/03/05 17:30:37  jijunwan
 *  Archive Log:    init version to support Application management
 *  Archive Log:    1) read/write opafm.xml from/to host with backup file support
 *  Archive Log:    2) Application parser
 *  Archive Log:    3) Add/remove and update Application
 *  Archive Log:    4) unique name, reference conflication check
 *  Archive Log:
 *
 *  Overview: 
 *
 *  @author: jijunwan
 *
 ******************************************************************************/

import java.io.File;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    public static void writeDoc(Document doc, File file) throws Exception {
        TransformerFactory transformerFactory = SAXTransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        // transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        // transformer.setOutputProperty(
        // "{http://xml.apache.org/xslt}indent-amount", "2");

        // XPathFactory xpathFactory = XPathFactory.newInstance();
        // // XPath to find empty text nodes.
        // XPathExpression xpathExp =
        // xpathFactory.newXPath().compile(
        // "//text()[normalize-space(.) = '']");
        // NodeList emptyTextNodes =
        // (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET);
        //
        // // Remove each empty text node from document.
        // for (int i = 0; i < emptyTextNodes.getLength(); i++) {
        // Node emptyTextNode = emptyTextNodes.item(i);
        // emptyTextNode.getParentNode().removeChild(emptyTextNode);
        // }

        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(file);
        transformer.transform(source, result);
    }
}

Related

  1. storeXml(final File xmlFile, final Document document)
  2. write(Document doc, File out)
  3. write(final Document doc, final File file)
  4. writeDataToFile(File file, Document doc)
  5. writeDoc(Document doc, File aFile)
  6. writeDocToFile(Document doc, String filename)
  7. writeDocToFile(Document doc, String fileName)
  8. writeDocument(Document doc, String filename)
  9. writeDocument(Document doc, String filePath)