Java XML Node to String toXMLString(final Node value)

Here you can find the source of toXMLString(final Node value)

Description

to XML String

License

Open Source License

Declaration

public static String toXMLString(final Node value) 

Method Source Code


//package com.java2s;
/*/*from w  w  w. ja  v  a2  s . c om*/
 * This file is part of Apparat.
 *
 * Copyright (C) 2010 Joa Ebert
 * http://www.joa-ebert.com/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import org.w3c.dom.Document;
import org.w3c.dom.Node;
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 java.io.StringWriter;

public class Main {
    public static String toXMLString(final Node value) {
        return toXMLString(new DOMSource(value));
    }

    public static String toXMLString(final Document value) {
        return toXMLString(new DOMSource(value));
    }

    public static String toXMLString(final DOMSource value) {
        final StringWriter writer = new StringWriter();
        try {
            final Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            transformer.transform(value, new StreamResult(writer));
        } catch (final Throwable t) {
            //todo throw flash error
            throw new RuntimeException(t);
        }

        final String result = writer.toString();

        return result.substring(0, result.length() - 1);
    }
}

Related

  1. toXML(Node node)
  2. toXml(Node node)
  3. toXml(Node node)
  4. toXML(Node node)
  5. toXml(Node node, boolean keepHeader)
  6. toXMLString(Node node)
  7. toXMLString(Node node)
  8. toXMLString(Node node)
  9. toXMLString(Node node, boolean header)