Java XML 输出 - 正确缩进子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2571866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:50:48  来源:igfitidea点击:

Java XML Output - proper indenting for child items

javaxmlindentation

提问by Dr1Ku

I'd like to serialize some simple data model into xml, I've been using the standard java.org.w3c -related code (see below), the indentation is better than no "OutputKeys.INDENT", yet there is one little thing that remains - proper indentation for child elements.

我想将一些简单的数据模型序列化为 xml,我一直在使用标准的 java.org.w3c 相关代码(见下文),缩进比没有“OutputKeys.INDENT”要好,但还有一点剩下的东西 - 子元素的适当缩进。

I know that this has been asked before on stackoverflow, yet that configuration did not work for me, this is the code I'm using :

我知道这之前曾在 stackoverflow上被问过,但该配置对我不起作用,这是我正在使用的代码:

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();

        doc = addItemsToDocument(doc);
        // The addItemsToDocument method adds childElements to the document.

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", new Integer(4));
        // switching to setAttribute("indent-number", 4); doesn't help

        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

         DOMSource source = new DOMSource(doc);
       StreamResult result = new StreamResult(outFile);
        // outFile is a regular File outFile = new File("some/path/foo.xml");

        transformer.transform(source, result);

The output produced is :

产生的输出是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<stuffcontainer>
<stuff description="something" duration="240" title="abc">
<otherstuff />
</stuff>
</stuffcontainer>

Whereas I would want it (for more clarity) like :

而我想要它(为了更清楚),例如:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<stuffcontainer>
  <stuff description="something" duration="240" title="abc">
    <otherstuff />
  </stuff>
</stuffcontainer>

I was just wondering if there is a way of doing this, make it properly indented for the child elements.

我只是想知道是否有办法做到这一点,让它为子元素正确缩进。

Thank you in advance !
Happy Easter coding :-) !

先感谢您 !
复活节快乐编码:-)!

回答by Chris Lercher

If the Transformer implementation you're using is Xalan-J, then you should be able to use:

如果您使用的 Transformer 实现是 Xalan-J,那么您应该能够使用:

transformer.setOutputProperty(
   "{http://xml.apache.org/xslt}indent-amount", "5");

See also: http://xml.apache.org/xalan-j/usagepatterns.html

另见:http: //xml.apache.org/xalan-j/usagepatterns.html

回答by Dr1Ku

import com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory

transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");

回答by iCrazybest

Document doc;

.....

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc), new StreamResult(new File("filename.xml")));
transformer.transform(new DOMSource(doc), new StreamResult(System.out));