English 中文(简体)
Java XML 产出——适当减少儿童物品
原标题:Java XML Output - proper indenting for child items

我愿将一些简单的数据模型编成Xml,我已使用标准java.org.w3c——相关代码(见下文),登革比“OutputKeys.INDENT”好,但仅剩一点东西——对儿童内容的适当tation。

我知道,在

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

产出如下:

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

我想(更清楚)这样:

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

我很想知道,是否有办法这样做,是否为儿童提供了适当保护。

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

最佳回答
问题回答
import com.sun.org.apache.xml.internal.serializer.OutputPropertiesFactory

transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");
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));




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签