App下載
首頁javadomJava HTML/XML - 如何漂亮的打印XML

Java HTML/XML - 如何漂亮的打印XML

我們想知道如何漂亮的打印XML。
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Node;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;

public class Main {

  public static String format(String xml) throws Exception {
    InputSource src = new InputSource(new StringReader(xml));
    Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
        .parse(src).getDocumentElement();
    Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));

    System.setProperty(DOMImplementationRegistry.PROPERTY,
        "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");

    DOMImplementationRegistry registry = DOMImplementationRegistry
        .newInstance();
    DOMImplementationLS impl = (DOMImplementationLS) registry
        .getDOMImplementation("LS");
    LSSerializer writer = impl.createLSSerializer();

    writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);
    return writer.writeToString(document);
  }

  public static void main(String[] args) throws Exception {
    String unformattedXml = "<?xml version='1.0' encoding='UTF-8'?><QueryMessage\n"
        + "        xmlns='http://www.SDMX.org/'\n"
        + "        xmlns:query='http://www.SDMX.org/query'>\n"
        + "    <Query>\n"
        + "        <query:CategorySchemeWhere>\n"
        + "           <query:AgencyID>ECB\n\n\n\n</query:AgencyID>\n"
        + "        </query:CategorySchemeWhere>\n"
        + "    </Query>\n\n\n\n\n"
        + "</QueryMessage>";

    System.out.println(format(unformattedXml));
  }
}