Java 8中的漂亮打印XML

问题描述 投票:22回答:7

我有一个存储为DOM文档的XML文件,我希望将其漂亮地打印到控制台上,最好不使用外部库。 [我知道这个问题已经在此站点上问过多次,但是以前的答案对我来说都没有。我正在使用Java 8,所以这也许是我的代码与以前的问题有所不同的地方?我也尝试过使用从Web上找到的代码手动设置变压器,但这只是导致了not found错误。

这是我的代码,当前仅在控制台左侧的新行上输出每个xml元素。

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


public class Test {
    public Test(){
        try {
            //java.lang.System.setProperty("javax.xml.transform.TransformerFactory", "org.apache.xalan.xsltc.trax.TransformerFactoryImpl");

            DocumentBuilderFactory dbFactory;
            DocumentBuilder dBuilder;
            Document original = null;
            try {
                dbFactory = DocumentBuilderFactory.newInstance();
                dBuilder = dbFactory.newDocumentBuilder();
                original = dBuilder.parse(new InputSource(new InputStreamReader(new FileInputStream("xml Store - Copy.xml"))));
            } catch (SAXException | IOException | ParserConfigurationException e) {
                e.printStackTrace();
            }
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory tf = TransformerFactory.newInstance();
            //tf.setAttribute("indent-number", 2);
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.transform(new DOMSource(original), xmlOutput);
            java.lang.System.out.println(xmlOutput.getWriter().toString());
        } catch (Exception ex) {
            throw new RuntimeException("Error converting to String", ex);
        }
    }

    public static void main(String[] args){
        new Test();
    }

}
java xml dom pretty-print transformer
7个回答
9
投票

我想这个问题与原始文件中的空白文本节点(即只有空白的文本节点)有关。您应该尝试在解析后使用以下代码以编程方式删除它们。如果不删除它们,则Transformer将保留它们。

original.getDocumentElement().normalize();
XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//text()[normalize-space(.) = '']");
NodeList blankTextNodes = (NodeList) xpath.evaluate(original, XPathConstants.NODESET);

for (int i = 0; i < blankTextNodes.getLength(); i++) {
     blankTextNodes.item(i).getParentNode().removeChild(blankTextNodes.item(i));
}

43
投票

为回复Espinosa的评论,这是“ 原始xml尚未(部分)缩进或包含新行的解决方案。

背景

启发该解决方案的文章摘录(请参阅下面的[[References]:]

根据DOM规范,标记外部的空格是完全有效的,并且已正确保留。要删除它们,我们可以使用XPath的normalize-space定位所有空白节点,然后首先将其删除。

Java代码public static String toPrettyString(String xml, int indent) { try { // Turn xml string into a document Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); // Remove whitespaces outside tags document.normalize(); XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } // Setup pretty print options TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // Return pretty print xml string StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(e); } }

样本用法

String xml = "<root>" + // "\n " + // "\n<name>Coco Puff</name>" + // "\n <total>10</total> </root>"; System.out.println(toPrettyString(xml, 4));

输出

<root> <name>Coco Puff</name> <total>10</total> </root>

参考文献

Save new XML node to file

3
投票
这在Java 8上有效:

2
投票
我已经写了simple class来删除文档中的空白-支持命令行,并且不使用DOM / XPath。

0
投票
我不喜欢任何一种常见的XML格式解决方案,因为它们都删除了多个连续的换行符(由于某些原因,删除空格/制表符和删除换行符是分不开的...)。这是我的解决方案,它实际上是为XHTML制作的,但也应该对XML起作用:

0
投票
Underscore-java8具有静态方法U.formatXml(string)。我是该项目的维护者。 Live example

-3
投票
创建xml文件:
© www.soinside.com 2019 - 2024. All rights reserved.