如何生成格式化的.xml文件?

时间:2015-12-01 09:41:15

标签: java

我正在编写一个生成xml文件的程序。代码完美地给出了预期的结果。但它只给我一个单行文档。我希望它以格式化的方式正确缩进。有没有办法做到这一点? 这是我使用的代码

public class Main {

    public static void main(String[] args) {
        WriteXml(7, 9, 0);
    }

    public static void WriteXml(int WS1, int FTP1, int EMAIL1) {

        try {
            final DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
            final DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
            final Document document = documentBuilder.newDocument();
            final Element root = document.createElement("Counter");
            document.appendChild(root);
            final Element properties = document.createElement("properties");
            root.appendChild(properties);

            final Element webservice = document.createElement("age");
            webservice.appendChild(document.createTextNode(Integer.toString(WS1)));
            properties.appendChild(webservice);

            final Element ftp = document.createElement("id");
            ftp.appendChild(document.createTextNode(Integer.toString(FTP1)));
            properties.appendChild(ftp);

            final Element email = document.createElement("grade");
            email.appendChild(document.createTextNode(Integer.toString(EMAIL1)));
            properties.appendChild(email);

            final TransformerFactory transformerFactory = TransformerFactory.newInstance();
            final Transformer transformer = transformerFactory.newTransformer();
            final DOMSource domSource = new DOMSource(document);
            final StreamResult streamResult = new StreamResult(new File("src/counter.xml"));
            transformer.transform(domSource, streamResult);

        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}

这给了我以下的xml文档

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Counter><properties><age>7</age><id>9</id><grade>0</grade></properties></Counter>

但我希望它更像是这个

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Counter>
    <properties>
        <age>7</age>
        <id>9</id>
        <grade>0</grade>
    </properties>
</Counter>

4 个答案:

答案 0 :(得分:3)

您可以在变换器的输出上设置多个属性,其中一个用于缩进:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");

答案 1 :(得分:2)

进行一些猜测,然后看this question添加这些行 获得变压器后可能会做到这一点

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

答案 2 :(得分:0)

Underscore-java库具有静态方法U.formatXml(xmlstring)。我是该项目的维护者。 Live example

import com.github.underscore.lodash.U;

String formattedXml = U.formatXml(xmlstring); 

答案 3 :(得分:0)

我创建了以下方法:

public String prettyPrintXml(String xmlStringToBeFormatted) {
    String formattedXmlString = null;
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setValidating(true);
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        InputSource inputSource = new InputSource(new StringReader(xmlStringToBeFormatted));
        Document document = documentBuilder.parse(inputSource);

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        StreamResult streamResult = new StreamResult(new StringWriter());
        DOMSource dOMSource = new DOMSource(document);
        transformer.transform(dOMSource, streamResult);
        formattedXmlString = streamResult.getWriter().toString().trim();
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    return formattedXmlString;
}