在Java 8中漂亮地打印XML

32

我有一个以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();
    }

}
7个回答

56

回复 Espinosa 的评论,这是一个解决方案,当 "原始的 xml 尚未(部分)缩进或包含换行符" 时使用。

背景

以下是激发该解决方案的文章摘录(请参阅下面的 参考资料):

  

根据 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>

参考资料


1
@btrs20 的区别在于去除空格。 - Stephan
1
我最终做了类似的事情,使用简单递归查找仅包含空格的文本节点,而没有使用XPath。你的代码更短。这是XPath高级用法的一个好例子。谢谢。 - Espinosa
1
如果这个工作完美无缺,但是如果你遇到了一些关于缺少“indend-number”属性的异常,解决方案就是检查类路径中是否有实现TransformerFactory的类。我在类路径中有一个名为“net.sf.saxon:Saxon-HE”的库,它定义了一个额外的TransformerFactory。 - raisercostin
1
删除空白很重要。如果您的字符串在行之间有空格,则转换器无法正常工作。 - Display name
2
@Marteng 你可以尝试使用underscore-java库和U.formatXml(xml)方法。 - Valentyn Kolesnikov
显示剩余9条评论

10

我想问题与原始文件中的空文本节点(即只有空格的文本节点)有关。您应该尝试在解析后使用以下代码程序化地将它们移除。如果不移除它们,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));
}

5

这段代码适用于Java 8:

public static void main (String[] args) throws Exception {
    String xmlString = "<hello><from>ME</from></hello>";
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.parse(new InputSource(new StringReader(xmlString)));
    pretty(document, System.out, 2);
}

private static void pretty(Document document, OutputStream outputStream, int indent) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    if (indent > 0) {
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
    }
    Result result = new StreamResult(outputStream);
    Source source = new DOMSource(document);
    transformer.transform(source, result);
}

嗯,那对我也有效,所以我猜问题肯定出在我读取xml文件的方式上。 - Hungry
4
注意,此解决方案仅适用于原始的XML未经(部分)缩进或包含换行符的情况。也就是说,它适用于“<hello><from>ME</from></hello>”,但不适用于“<hello>\n<from>ME</from>\n</hello>”。 - Espinosa
1
对于普通读者,这里有一个解决@Espinosa警告的方案:https://dev59.com/sV8e5IYBdhLWcg3wVJEC#33541820 - Stephan

2
我编写了一个 简单的类,用于在文档中删除空格 - 支持命令行且不使用 DOM / XPath。
编辑:想一想,该项目还包含一个处理现有空格的漂亮打印机:
PrettyPrinter prettyPrinter = PrettyPrinterBuilder.newPrettyPrinter().ignoreWhitespace().build();

1

Underscore-java有一个静态方法U.formatXml(string)。我是这个项目的维护者。实时示例

import com.github.underscore.U;

public class MyClass {
    public static void main(String args[]) {
        String xml = "<root>" + //
             "\n   "  + //
             "\n<name>Coco Puff</name>" + //
             "\n        <total>10</total>    </root>";

        System.out.println(U.formatXml(xml));
    }
}

输出:

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

0

我不喜欢任何常见的XML格式化解决方案,因为它们都会删除超过1个连续换行符(由于某种原因,删除空格/制表符和删除换行符是不可分割的...)。这是我的解决方案,实际上是为XHTML而设计的,但也适用于XML:

public String GenerateTabs(int tabLevel) {
  char[] tabs = new char[tabLevel * 2];
  Arrays.fill(tabs, ' ');

  //Or:
  //char[] tabs = new char[tabLevel];
  //Arrays.fill(tabs, '\t');

  return new String(tabs);
}

public String FormatXHTMLCode(String code) {
  // Split on new lines.
  String[] splitLines = code.split("\\n", 0);

  int tabLevel = 0;

  // Go through each line.
  for (int lineNum = 0; lineNum < splitLines.length; ++lineNum) {
    String currentLine = splitLines[lineNum];

    if (currentLine.trim().isEmpty()) {
      splitLines[lineNum] = "";
    } else if (currentLine.matches(".*<[^/!][^<>]+?(?<!/)>?")) {
      splitLines[lineNum] = GenerateTabs(tabLevel) + splitLines[lineNum];

      ++tabLevel;
    } else if (currentLine.matches(".*</[^<>]+?>")) {
      --tabLevel;

      if (tabLevel < 0) {
        tabLevel = 0;
      }

      splitLines[lineNum] = GenerateTabs(tabLevel) + splitLines[lineNum];
    } else if (currentLine.matches("[^<>]*?/>")) {
      splitLines[lineNum] = GenerateTabs(tabLevel) + splitLines[lineNum];

      --tabLevel;

      if (tabLevel < 0) {
        tabLevel = 0;
      }
    } else {
      splitLines[lineNum] = GenerateTabs(tabLevel) + splitLines[lineNum];
    }
  }

  return String.join("\n", splitLines);
}

它有一个假设:除了组成XML/XHTML标签的字符外,没有其他<>字符。


1
这段代码不完整,因为无法解析codeGenerator变量。对应的类是用Java编写的吗?因为Java方法名称确实有不同的命名约定。 - benez
@benez 很抱歉,感谢您告诉我这个问题。我没有意识到有外部代码被使用。请尝试一下,我认为它会起作用;现在无法测试。 - Andrew

-3
创建 XML 文件:
new FileInputStream("xml Store - Copy.xml") ;// result xml file format incorrect ! 

这样,当解析给定输入源的内容作为XML文档并返回一个新的DOM对象。

Document original = null;
...
original.parse("data.xml");//input source as an XML document

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接