Java XML String Transform writeXML(Node node, String dtdFilename, String outputFileName)

Here you can find the source of writeXML(Node node, String dtdFilename, String outputFileName)

Description

write XML

License

Open Source License

Declaration

public static void writeXML(Node node, String dtdFilename,
            String outputFileName) throws TransformerException, IOException 

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave.//from  w  w w .jav a 2  s  . com
 *
 * The Initial Developer of Weave is the Institute for Visualization
 * and Perception Research at the University of Massachusetts Lowell.
 * Portions created by the Initial Developer are Copyright (C) 2008-2015
 * the Initial Developer. All Rights Reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * ***** END LICENSE BLOCK ***** */

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

import java.io.StringWriter;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class Main {
    public static void writeXML(Node node, String dtdFilename,
            String outputFileName) throws TransformerException, IOException {
        getStringFromXML(node, dtdFilename, outputFileName);
    }

    public static String getStringFromXML(Node node)
            throws TransformerException {
        return getStringFromXML(node, null);
    }

    public static String getStringFromXML(Node node, String dtdFilename)
            throws TransformerException {
        StringWriter sw = new StringWriter();
        getStringFromXML(node, dtdFilename, sw);
        return sw.getBuffer().toString();
    }

    public static void getStringFromXML(Node node, String dtdFilename,
            String outputFileName) throws TransformerException, IOException {
        File file = new File(outputFileName);
        if (!file.isFile())
            file.createNewFile();
        BufferedWriter out = new BufferedWriter(new FileWriter(file));

        String workingPath = System.setProperty("user.dir", file
                .getAbsoluteFile().getParent());
        try {
            getStringFromXML(node, dtdFilename, out);
        } finally {
            System.setProperty("user.dir", workingPath);
        }

        out.flush();
        out.close();
    }

    public static void getStringFromXML(Node node, String dtdFilename,
            Writer outputWriter) throws TransformerException {
        File dtdFile = null;
        String workingPath = null;
        if (dtdFilename != null) {
            dtdFile = new File(dtdFilename);
            workingPath = System.setProperty("user.dir", dtdFile
                    .getAbsoluteFile().getParent());
        }
        try {
            if (node instanceof Document)
                node = ((Document) node).getDocumentElement();

            TransformerFactory tranFact = TransformerFactory.newInstance();
            Transformer tf = tranFact.newTransformer();
            if (dtdFile != null) {
                tf.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                        dtdFile.getName());
                tf.setOutputProperty(OutputKeys.INDENT, "yes");
            }

            Source src = new DOMSource(node);
            Result dest = new StreamResult(outputWriter);

            tf.transform(src, dest);
        } finally {
            if (workingPath != null)
                System.setProperty("user.dir", workingPath);
        }
    }
}

Related

  1. writeElement(Element element, String fileName)
  2. writeNodeSubtreeXMLString(final Node node, final Writer writer)
  3. writePropToString(Properties props)
  4. writeResultToFile(final StreamResult result, final String path)
  5. writeToFile(String xmlContent, String path)
  6. writeXml(OutputStream os, Node node, String encoding)
  7. writeXmlFile(Element doc, String filename)
  8. writeXmlResult(String xml, Writer w)
  9. xslString(final String xmlFile, final InputStream xslStream)