views:

106

answers:

2

I have used javax.xml.transform.Transformer and javax.xml.transform.TransformerFactory and org.w3c.dom.Element and org.w3c.dom.Node to create XML file as per my requirement. Its creating XML successfully. The only problem is :

<MYADDRESS NAME="AA00001">
<ATTN1>a</ATTN1>
<ADDRESS></ADDRESS> // This is empty ADDRESS element/tag.
<STATE>AA</STATE>
<ZIP>1</ZIP>
</MYADDRESS>

The <ADDRESS></ADDRESS> shows in browser as <ADDRESS/> where as in editor like notepad, wordpad it is shown as <ADDRESS><ADDRESS>. I want it to be displayed as <ADDRESS/> when the file is opened in Editor also.

Any idea ? Thanks for help !

+1  A: 

You are aware, that <ADDRESS></ADDRESS> and <ADDRESS/> are identical, right? The browser just happens to collapse the former into the latter. From the point of view of an XML parser, there's no difference.

mwittrock
A: 

I think you want to configure the transform to collapse empty elements in the serialized output (like the text file). A quick look at the list of properties shows, that there is no such option.

So I think you'll have to implement and provide your own content handler (xalan:content-handler property). Maybe you can just subclass the default one and change the serialization behaviour for empty elements.

Andreas_D
Thank you both for the response. At http://www.jdom.org/docs/apidocs/org/jdom/output/Format.htmlthere is method setExpandEmptyElements which looks like working as per need. But is there any direct way to do it for javax.xml.transform.Transformer and org.w3c.dom ?
Ronak
I doubt that. This Formatter just encapsulates XMLOutputter format options. And the XMLOutputter is designed to work with `org.jdom.Document` objects but you use the `org.w3c.dom.Document` type. jdom has a class to convert from `org.jdom.Document` to `org.w3c.dom.Document` but not vice versa.
Andreas_D