我在下面编写了代码来获取 XML 输出。
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element element = document.createElement("Test");
Text text = document.createTextNode("");
element.appendChild(text);
document.appendChild(element);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
我得到的是
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Test/>
我想要得到的是
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Test></Test>
我怎样才能做到这一点?
非常感谢。