views:

38

answers:

1

Hello, 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

+1  A: 

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.

Jon Freedman
The indent in the second line is too small, should be [n+i space characters]
Andreas_D
well spotted! I've fixed it
Jon Freedman
This is what I'm coding right now, but I was in hope of something like transformer.setOutputProperty(OutputKeys.PUT_STRING_CHILDREN_ON_NEW_LINE, "yes");
ADR
The main point I'm trying to get accross is that the two documents are not equal, and if you actually want text with the new line character in you are going to get one hell of a mess... You should give whoever wrote your fortran library a slap :)
Jon Freedman
The documents are different *but* the data itself is equal when you actually read the text children and trim it. I'm almost done with writing my own code to do that :(
ADR