Java XML Node Transform createXML(Node doc)

Here you can find the source of createXML(Node doc)

Description

create XML

License

Open Source License

Declaration

public static String createXML(Node doc) 

Method Source Code

//package com.java2s;

import java.io.ByteArrayOutputStream;

import java.io.OutputStream;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    public static String createXML(Node doc) {
        TransformerFactory xff = TransformerFactory.newInstance();
        Transformer serializer = null;
        try {//from  www  .ja  v  a2s  .com
            serializer = xff.newTransformer();
        } catch (TransformerConfigurationException te) {
            throw new RuntimeException("Could not create transformer. " + te.getMessage());
        }

        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://saxon.sf.net/}indent-spaces", "4");
        serializer.setOutputProperty(OutputKeys.METHOD, "xml");
        OutputStream os = new ByteArrayOutputStream();
        try {
            serializer.transform(new DOMSource(doc), new StreamResult(os));
            os.close();
        } catch (Exception e) {
            throw new RuntimeException("Could not create transformer.", e);
        }

        return os.toString();
    }
}

Related

  1. buildXml(Node node)
  2. copyTreeToResult(Node tree, Result result)
  3. createInputStream(Node node)
  4. getAsText(Node n)
  5. getHumanReadableXml(final Node node)
  6. getNodeValue(NodeList nodeList)
  7. getValueOfValueNode(Node n, boolean unescape)