Java XML Node to String getStringFromXML(Node node)

Here you can find the source of getStringFromXML(Node node)

Description

get String From XML

License

Open Source License

Declaration

public static String getStringFromXML(Node node)
            throws TransformerException 

Method Source Code

//package com.java2s;
/* ***** BEGIN LICENSE BLOCK *****
 *
 * This file is part of Weave./*from ww w. ja  v a 2 s .  c o  m*/
 *
 * 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 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. getOuterXml(Node n)
  2. GetOuterXml(Node node)
  3. getStringFromDocument(Node doc)
  4. getStringFromNode(Node node)
  5. getStringFromNode(Node node)
  6. getText(final Node node)
  7. getXml(Node node)
  8. getXmlAsString(Node node)
  9. getXMLString(Node doc)