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.Transformer;
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.Document;

public class Main {
    /**
     * Serialize an XML document into a String (for debug purpose only!). the
     * returned String could contains an error message instead if a problem as
     * occurred during the serialization. This method is intended for debug /
     * trace purpose because you really don't need to serialize XML at all in
     * your code unless your planing to output it to a file. In this case, use
     * {@link XmlHelper#writeXmlToFile(Document, File)} instead.
     */
    public static String dumpXml(Document document) {
        TransformerFactory factory = TransformerFactory.newInstance();
        try {
            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(document), new StreamResult(writer));
            return writer.getBuffer().toString();
        } catch (TransformerException e) {
            return "XML dump failed: " + e.getMessage();
        }
    }
}