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 {
    public static String formatXmlAsStringNoWhitespace(Document document) {

        StringWriter writer = new StringWriter();
        TransformerFactory factory = TransformerFactory.newInstance();

        try {

            Transformer transformer = factory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "no");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

            transformer.transform(new DOMSource(document), new StreamResult(writer));
        } catch (TransformerException ex) {

            throw new RuntimeException("Error formatting xml as no-whitespace string", ex);
        }

        return writer.toString();
    }
}