简体   繁体   中英

How to prevent self-closing <tags/> in 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):

Original:

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

After edit:

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

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM