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.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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 {
    /**
     * Creates a string representation of a {@link Node} instance. This method
     * does not introduce any character to the string representation of the
     * {@link Node} (eg. \n or \r characters)
     *
     * @param node A {@link Node} instance
     * @return A string representation of the node instance
     * @throws RequestSecurityTokenException
     */
    public static String xmlToString(Node node) throws Exception {
        try {
            Source source = new DOMSource(node);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();

        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
            throw new Exception("Cannot build a string representation of the assertion.");
        } catch (TransformerException e) {
            e.printStackTrace();
            throw new Exception("Cannot build a string representation of the assertion.");
        }
    }
}