Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何生成格式化的.xml文件?_Java - Fatal编程技术网

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

Java 如何生成格式化的.xml文件?,java,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) {

我正在编写一个生成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>
790
但我希望它更像这样

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

7.
9
0

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

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

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

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

通过一些猜测,在看了添加这些行之后 在获得变压器后,可能会达到目的

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

通过一些猜测,在看了添加这些行之后 在获得变压器后,可能会达到目的

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

我创建了以下方法:

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;
}

我创建了以下方法:

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;
}

下划线java库的可能副本具有静态方法U.formatXml(xmlstring)。下划线java库的可能副本具有静态方法U.formatXml(xmlstring)。