javax.xml.transform.Transformer 出现问题。空元素在编辑器中但在浏览器中无法正确显示
我已经使用 javax.xml.transform.Transformer 和 javax.xml.transform.TransformerFactory 以及 org.w3c.dom.Element 和 org.w3c.dom.Node 根据我的要求创建 XML 文件。其创建 XML 成功。唯一的问题是:
// 这是空的 ADDRESS 元素/标签。
<状态>AA
<地址>在浏览器中显示为
>在记事本、写字板等编辑器中,它显示为。我希望它显示为>当文件在编辑器中打开时。有什么想法吗? 感谢您的帮助!
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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您知道,
和
是相同的,对吗?浏览器恰好将前者折叠成后者。从 XML 解析器的角度来看,没有什么区别。
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.我认为您想要配置转换以折叠序列化输出中的空元素(如文本文件)。快速查看属性列表显示,没有这样的选择。
因此,我认为您必须实现并提供自己的内容处理程序(
xalan:content-handler
属性)。也许您可以只对默认元素进行子类化并更改空元素的序列化行为。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.