Java XML Document Save to File saveXML(Document doc, File outfile, boolean indent)

Here you can find the source of saveXML(Document doc, File outfile, boolean indent)

Description

save XML

License

Open Source License

Declaration

@SuppressWarnings("UseSpecificCatch")
    public static boolean saveXML(Document doc, File outfile, boolean indent) 

Method Source Code


//package com.java2s;
/* Copyright (c) 2016 by crossmobile.org
 *
 * CrossMobile is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.
 *
 * CrossMobile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CrossMobile; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *//*from  w w  w.  j a  v a  2s  .c  om*/

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    @SuppressWarnings("UseSpecificCatch")
    public static boolean saveXML(Document doc, File outfile, boolean indent) {
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"));
            if (indent) {
                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);
                }
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            }
            transformer.transform(source, result);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
}

Related

  1. saveToFile(Document doc, File f, boolean indent)
  2. saveToFile(String filename, Document document)
  3. saveToXml(Document doc, OutputStream os)
  4. saveToXmlStr(Document doc)
  5. saveXML(Document doc, ByteArrayOutputStream outputStreamXML)
  6. saveXml(Document doc, String filename)
  7. saveXML(Document document, String filename)
  8. saveXml(Document dom, String filePath)
  9. saveXml(Document modDoc, String path)