如何防止自闭合< tags/>在XML中? [英] How to prevent self-closing <tags/> in XML?

查看:228
本文介绍了如何防止自闭合< tags/>在XML中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Transformer类和transform方法修改XML文件.它正确地修改了我的参数,但是改变了XML样式(以不同的方式写入XML属性):

I modify XML file using the Transformer class and transform method. It correctly modify my parameters but changed XML style (write XML attributes in different way):

原文:

<a struct="b"></a>
<c></c>

编辑后:

<a struct="b"/>
<c/>

我知道我可以设置属性:transformer.setOutputProperty(OutputKeys.KEY,value),,但是找不到合适的设置.

I know that I can set properties: transformer.setOutputProperty(OutputKeys.KEY,value), but I did not find proper settings.

任何人都可以帮助转换器不更改写入格式吗?

Can anyone help the transformer not change the write format?

XMLReader xr = new XMLFilterImpl(XMLReaderFactory.createXMLReader()
Source src = new SAXSource(xr, new InputSource(new 
StringReader(xmlArray[i])));
<<modify xml>>
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();          
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
           StringWriter buffer = new StringWriter();
            transformer.transform(src, new StreamResult(buffer));
            xmlArray[i] = buffer.toString();

推荐答案

这些形式在语义上是等效的.没有符合条件的XML解析器会在乎,您也不应该.

Those forms are semantically equivalent. No conforming XML parser will care, and neither should you.

这篇关于如何防止自闭合&lt; tags/&gt;在XML中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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