我有这个密码:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
Document xmldoc = impl.createDocument(null, null, null);
Element root = xmldoc.createElement("root");
Element textElement = xmldoc.createElement("text");
Text textNode = xmldoc.createTextNode("");
root.appendChild(textElement);
textElement.appendChild(textNode);
OutputFormat of = new OutputFormat("XML","UTF-8",true);
of.setIndent(1);
of.setIndenting(true);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
XMLSerializer serializer = new XMLSerializer(stream, of);
// As a DOM Serializer
serializer.asDOMSerializer();
serializer.serialize(root);
System.out.println(stream.toString());
我就能控制住这一切:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<text/>
</root>
但是,我喜欢得到这个:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<text></text>
</root>
Does anybody know, how to set the XMLSerializer to avoid Complex Empty Elements? Thanks.