Alessandro Da Rugna
Alessandro Da Rugna

Reputation: 4695

Indent text child nodes with javax.xml.transform.Transformer

I'm transforming a DOM document to XML in java using javax.xml APIs. The result is

<tag>
    <tag2>text</tag2>
</tag>

but I need it to be

<tag>
    <tag2>
        text
    </tag2>
</tag>

Are there any options to do that with text child nodes? I didn't find any

Upvotes: 0

Views: 619

Answers (1)

Jon Freedman
Jon Freedman

Reputation: 9697

I think you will need to write your own marshaller to do this, as assuming you want this formatting to apply to all levels of XML you want to take the String text and transform it into

[line feed character]
[n + i space characters]text[line feed character]
[n space characters]

where n depends on the depth of the current element from the root element.

Explicitly, if you compare the values of xpath /tag/tag2 on both these documents they are not the same.

Upvotes: 1

Related Questions