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.File;
import java.io.FileWriter;
import java.io.Writer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

public class Main {
    /**
     * Formats a file nice looking
     * @param file The XML file to format
     * @throws Exception If the file isn't an XML file
     */
    public static void format(File file) throws Exception {
        if (file.isFile()) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = null;
            Document document = null;
            builder = factory.newDocumentBuilder();
            document = builder.parse(file);

            OutputFormat format = new OutputFormat(document);
            format.setLineWidth(65);
            format.setIndenting(true);
            format.setIndent(2);
            Writer out = new FileWriter(file);
            XMLSerializer serializer = new XMLSerializer(out, format);
            serializer.serialize(document);
            out.close();
        }
    }
}