Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Node;

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

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

public class Main {
    public static String xml(Node node) {
        TransformerFactory factory = TransformerFactory.newInstance();
        return xml(factory, node);
    }

    public static String xml(TransformerFactory transformerFactory, Node node) {
        Writer writer = new StringWriter();
        DOMSource source = new DOMSource(node);
        StreamResult result = new StreamResult(writer);
        try {
            transformerFactory.newTransformer().transform(source, result);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
        return writer.toString().replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
    }
}