[英]How to remove Extra indent in XML Dom in Java
我正在 java 中編寫一個方法,它將數據附加到以下 XML 文件中:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Esko>
<Employees>
</Employees>
</Esko>
在這里,我想將員工數據附加到這個員工標簽上,就像這樣。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MyOrg>
<Employees>
<Employee>
<Name>gaurav kumar </Name>
<Designation>SSSSS</Designation>
</Employee>
<Employee>
<Name>gaurav kumar </Name>
<Designation>SSSSS</Designation>
</Employee>
<Employee>
<Name>gaurav</Name>
<Designation>singh</Designation>
</Employee>
</Employees>
</MyOrg>
為此,我正在使用此代碼:
public void saveToXML(String xml) {
Document dom;
NodeList employees = null;
Node eRoot = null;
// instance of a DocumentBuilderFactory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// use factory to get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
// create instance of DOM
dom = db.parse(xml);
// create the root element
// Element rootEle = dom.getElementByName("Esko");
// create data elements and place them under root
employees = dom.getElementsByTagName("Employees");
System.out.println(employees.item(0));
eRoot = employees.item(0);
eRoot.appendChild(createEmployee(dom, nameField.getText(), designationField.getText()));
try {
Transformer tr = TransformerFactory.newInstance().newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
// send DOM to file
tr.transform(new DOMSource(dom),
new StreamResult(new FileWriter(xml)));
} catch (TransformerException te) {
System.out.println(te.getMessage());
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
} catch (ParserConfigurationException pce) {
System.out.println("UsersXML: Error trying to instantiate DocumentBuilder " + pce);
} catch (SAXException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
private Element createEmployee(Document dom, String name, String des)
{
Element e = dom.createElement("Employee");
Element eName = dom.createElement("Name");
Element eDes = dom.createElement("Designation");
//create name and designation as child tag of Employee tag
e.appendChild(eName);
e.appendChild(eDes);
// name tag contain what is supplied by the user at name field
eName.appendChild(dom.createTextNode(name));
// designation tag contains the designation by the user
eDes.appendChild(dom.createTextNode(des));
return e;
}
但現在發生的情況是,每次我保存一名新員工時,都會像這樣向 XML 增加額外的縮進。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MyOrg>
<Employees>
<Employee>
<Name>gaurav</Name>
<Designation>SDET</Designation>
</Employee>
<Employee>
<Name>gaurav kumar</Name>
<Designation>SDET2</Designation>
</Employee>
</Employees>
</MyOrg>
我不確定它的原因,但它正在發生,因為我每次保存到 xml 時都設置縮進。 任何人都可以幫助我為什么會發生這種情況,以及如何在沒有這些額外縮進的情況下保存它。
可以使用以下輔助函數從 XML 文檔中刪除空格:
public static void removeWhitespaces(Element element) {
NodeList children = element.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--) {
Node child = children.item(i);
if (child instanceof Text
&& ((Text) child).getData().trim().isEmpty()) {
element.removeChild(child);
} else if (child instanceof Element) {
removeWhitespaces((Element) child);
}
}
}
它可以被稱為removeWhitespaces(dom.getDocumentElement());
在調用tr.transform()
之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.