Java XML Node to String toXml(Node node)

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

Description

Returns String representation of the the XML node (document or element).

License

Open Source License

Parameter

Parameter Description
node the node

Exception

Parameter Description
Exception if an error occurs

Return

string representation of the input

Declaration

public static String toXml(Node node) throws Exception 

Method Source Code

//package com.java2s;
/**//from  ww  w  .ja  v  a2s . c o m
 * The MIT License
 * Copyright (c) 2015 Estonian Information System Authority (RIA), Population Register Centre (VRK)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import org.w3c.dom.Node;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
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 {
    /**
     * Returns String representation of the the XML node (document or element).
     * @param node the node
     * @return string representation of the input
     * @throws Exception if an error occurs
     */
    public static String toXml(Node node) throws Exception {
        Source source = new DOMSource(node);
        StringWriter writer = new StringWriter();
        Result result = new StreamResult(writer);
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.transform(source, result);

        return writer.toString();
    }
}

Related

  1. toText(Node node)
  2. toWellKnowName(Node vertexStyleNode)
  3. toWriter(Node node, Writer writer, Map outputProperties)
  4. toXML(Node node)
  5. toXML(Node node)
  6. toXml(Node node)
  7. toXml(Node node, boolean keepHeader)
  8. toXMLString(final Node value)
  9. toXMLString(Node node)