Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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 {
    private static Transformer tx = null;
    private static boolean isInited = false;

    public static String nodeToString(Node n) {
        init();
        try {
            DOMSource src = new DOMSource(n);
            StringWriter sr = new StringWriter();
            Result res = new StreamResult(sr);
            tx.transform(src, res);
            return sr.toString();
        } catch (Exception e) {
            return (e.getMessage());
        }
    }

    public static void init() {
        if (isInited) {
            return;
        }
        try {
            tx = TransformerFactory.newInstance().newTransformer();
            tx.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            tx.setOutputProperty(OutputKeys.INDENT, "no");
            isInited = true;
        } catch (TransformerFactoryConfigurationError | TransformerConfigurationException
                | IllegalArgumentException e) {
            e.printStackTrace();
        }
    }
}