防止空的 xml 元素转换为自关闭元素 [英] preventing empty xml elements are converted to self closing elements

查看:24
本文介绍了防止空的 xml 元素转换为自关闭元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xerces 库在文档中编写 XML.为此,我使用 OutputFormat 类在 XMLSerializer 中传递 OutputFormat 的对象.但是我所有的空 xml 元素都转换为自关闭 xml 元素.

I am using Xerces Library for writing XML in document . For that I am using OutputFormat class passing the object of OutputFormat in XMLSerializer. But all of my empty xml elements are converted to self-closing xml elements.

我想要这个:

<Company Name="Dummy">
</Company>

但它来了

<Company Name="Dummy" />

我尝试过以下代码:

try {
    //print
    OutputFormat format = new OutputFormat(dom,"iso-8859-1",true);          
    //to generate output to console use this serializer

    XMLSerializer serializer = new XMLSerializer(System.out, format);           
    serializer.serialize(dom);

} catch(IOException ie) {
        ie.printStackTrace();
}

有人可以帮我解决这个问题吗?

Can someone help me out on this.

谢谢,

推荐答案

我所知道的大多数序列化程序都不允许您选择是否在输出中使用空元素标签,原因很简单,即没有理智的 XML 消费者应该关心它们是否被使用.如果您确实在意,并且没有精神错乱,这将有助于解释您为何在意.

Most serializers I know of do not allow you to choose whether or not to use empty element tags in the output, for the simple reason that no sane consumer of XML should care whether they are used or not. If you do care, and are not insane, it would help to explain why you care.

这篇关于防止空的 xml 元素转换为自关闭元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆