开发者

Problem with javax.xml.transform.Transformer. Empty element not showing properly in editor but in Browser

开发者 https://www.devze.com 2023-01-11 10:25 出处:网络
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 successf开

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 successf开发者_开发知识库ully. 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 !


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消