Java语言实现XML美化功能

原创
08/16 17:48
阅读数 13

Java语言实现XML美化功能

代码如下:

package tools;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

/**
 * XML格式美化工具
 * 
 * @author Tsybius2014
 *
 */
public class XmlFormatTool {

    /** 被美化XML文件的地址 */
    public static String INPUT_FILE_PATH = "C:\\Users\\Tsybius2014\\Desktop\\input.xml";

    /** 美化后XML文件的地址 */
    public static String OUTPUT_FILE_PATH = "C:\\Users\\Tsybius2014\\Desktop\\output.xml";

    /**
     * XML格式美化工具
     * 
     * @param args
     *            参数列表
     */
    public static void main(String[] args) {

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {

            fileInputStream = new FileInputStream(INPUT_FILE_PATH);
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(fileInputStream);

            DOMImplementationRegistry domImplementationRegistry = DOMImplementationRegistry.newInstance();
            DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementationRegistry
                    .getDOMImplementation("XML 3.0");
            LSSerializer lsSerializer = domImplementationLS.createLSSerializer();

            DOMConfiguration domConfiguration = lsSerializer.getDomConfig();
            boolean isSupport = domConfiguration.canSetParameter("format-pretty-print", true);
            if (isSupport) {
                domConfiguration.setParameter("format-pretty-print", true);
            }

            LSOutput lsOutput = domImplementationLS.createLSOutput();
            lsOutput.setEncoding("UTF-8");

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            lsOutput.setByteStream(byteArrayOutputStream);
            lsSerializer.write(document, lsOutput);
            byte[] data = byteArrayOutputStream.toByteArray();

            File outputFile = new File(OUTPUT_FILE_PATH);
            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }

            fileOutputStream = new FileOutputStream(outputFile);
            fileOutputStream.write(data);

            System.out.println("文件已生成:" + outputFile.getAbsolutePath());

        } catch (Exception e) {
            System.out.println("异常:" + e.getMessage());
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    System.out.println("fileInputStream关闭失败:" + e.getMessage());
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    System.out.println("fileOutputStream关闭失败:" + e.getMessage());
                }
            }
        }

    }
}

美化前文件:

美化后文件:

END

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部