Java XML Node Transform buildXml(Node node)

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

Description

build Xml

License

Open Source License

Declaration

public static String buildXml(Node node) throws TransformerFactoryConfigurationError, TransformerException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2011 eBay Inc./*from  ww w. j  a va  2 s .  co m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *******************************************************************************/

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

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    public static String buildXml(Node node) throws TransformerFactoryConfigurationError, TransformerException {
        StringWriter writer = new StringWriter();

        buildXml(node, writer);
        return writer.toString();
    }

    public static void buildXml(Node node, Writer writer)
            throws TransformerFactoryConfigurationError, TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        transformer.transform(new DOMSource(node), new StreamResult(writer));
    }
}

Related

  1. copyTreeToResult(Node tree, Result result)
  2. createInputStream(Node node)
  3. createXML(Node doc)
  4. getAsText(Node n)