views:

57

answers:

3

Hi,

I'm using javax.xml.transform.Transformer class to transform the DOM source into XML string. I have some empty elements in DOM tree, and these become one tag which I don't want.

How do I prevent <sampletag></sampletag> from becoming <sampletag/>?

+2  A: 

If you want to control how XML is formatted, provide your own ContentHandler to prettify XML into "text". It should not matter to the receiving end (unless human) whether it receives <name></name> or <name/> - they both mean the same thing.

Tom Hawtin - tackline
This xml will then be converted into a pdf, and I don't have any control on the pdf converter method, so I need the tags to preserve their usage.
A: 

The two representations are equivalent to an XML parser, so it doesn't matter.

If you want to process XML with anything else than an XML-parser, you will end up with a lot of work and an XML-parser anyway.

Thorbjørn Ravn Andersen
A: 

If the process you are sending it through NEEDS the element not to be self-closing (which it should not), you can force the element not to be self-closing by placing content inside of it.

How does the PDF converter handle XML comments or processing instructions?

<sampletag>!<--Sample Comment--></sampletag>

<sampletag><?SampleProcessingInstruction?></sampletag>

Mads Hansen