java - java 8中 pretty-print XML

标签 java xml dom pretty-print

我有一个存储为 DOM 文档的 XML 文件,我想将它漂亮地打印到控制台,最好不使用外部库。 我知道这个问题已经在这个网站上被问过多次,但是以前的答案都没有对我有用。我使用的是 java 8,所以也许这是我的代码与以前的问题不同的地方?我也尝试使用从网上找到的代码手动设置转换器,但这只会导致 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();
    }

}

最佳答案

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

背景

摘自启发此解决方案的文章(请参阅下面的引用资料):

Based on the DOM specification, whitespaces outside the tags are perfectly valid and they are properly preserved. To remove them, we can use XPath’s normalize-space to locate all the whitespace nodes and remove them first.

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>

引用文献

关于java - java 8中 pretty-print XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25864316/

相关文章:

java - 如何使用 java sdk 查找 Google DataFlow 中每个步骤所花费的总执行时间

java - 用户注册与 Facebook 连接

c# - 在 Unity 中使用 XML 存储颜色和字符串

javascript - 用于通过::after 修饰符添加的内容的 DOM 选择器

javascript - 为什么甚至动态插入对象的处理程序都不会触发,即使它们在 JavaScript 运行之前就已存在?

java - Android中如何从ListView中获取当前所选项目的背景颜色

java - 从充满 POJO 的 ArrayList 中删除重复项

python-3.x - 如何访问child的xml文本?

c# - 将 XML 数据保存到 SQL Server 表

JavaScript 和 DOM 按钮事件处理