Java DOM:具有多行段落的缩进文档元素

时间:2015-05-28 05:53:24

标签: java dom

要在Java(DOM)中缩进XML Document输出,我使用以下代码,大家都知道,

TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
StringWriter sw = new StringWriter();
trans.transform(new DOMSource(parent), new StreamResult(sw));

对于较小的文本内容,它没有任何问题。但是当我的元素/节点中存在大量文本内容时(特别是我的文档'元素中有大段内容),它没有正确缩进。我得到的输出如下,

<xs:annotation>
       <xs:documentation>"The value of sysUpTime at the time of the last change of
        the (whole) interface stack.  A change of the interface
        stack is defined to be any creation, deletion, or change in
        value of any instance of ifStackStatus.  If the interface
        stack has been unchanged since the last re-initialization of
        the local network management subsystem, then this object
        contains a zero value."</xs:documentation>
</xs:annotation>

请帮助我对这些段落进行适当的格式化/缩进。

我希望输出像,

<xs:annotation>
    <xs:documentation>
        "The value of sysUpTime at the time of the last change of
        the (whole)interface stack.  A change of the interface
        stack is defined to be any creation, deletion, or change in
        value of any instance of ifStackStatus.If the interface
        stack has been unchanged since the last re-initialization of
        the local network management subsystem, then this object
        contains a zero value."
     </xs:documentation>
</xs:annotation>

提前致谢!

0 个答案:

没有答案