Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

import java.io.IOException;

import java.io.StringWriter;

import org.w3c.dom.Document;

public class Main {
    /** Return a <code>String</code> containing an XML file in serialized form.
     *  @param  doc  the <code>Document</code> object for the XML file to list
     *  @return (<code>String</code>) the XML file in serialized form
     */
    public static String getXMLListing(Document doc) {
        // make a serializable version of the XML Document object
        // reference:  www-106.ibm.com/developerworks/xml/library/x-injava2/?dwzone=xml
        OutputFormat of = new OutputFormat(doc);
        of.setIndenting(true); // setIndenting must preceed setIndent
        of.setIndent(2);
        of.setLineSeparator(System.getProperty("line.separator"));
        // of.setPreserveSpace( true ) ;
        StringWriter sw = new StringWriter();
        // use a StringWriter instead of serializing directly to the 
        //  ObjectOutputStream because serializing directly yields a 
        //  java.io.OptionalDataException when reading the data with 
        //  ObjectInputStream.readObject()
        XMLSerializer xmlser = new XMLSerializer(sw, of);
        try {
            xmlser.serialize(doc);
        } catch (IOException ioe) {
            return "<p>IOException Error in getXMLListing( Document ):<br/>" + "&nbsp;&nbsp;&nbsp;"
                    + ioe.toString();
        }

        return sw.toString();
    }
}