JDOM编辑XML文件示例
时间:2020-02-23 14:35:19 来源:igfitidea点击:
JDOM提供了一种非常整洁的方式来处理XML文件,使用JDOM非常容易,并且代码看起来清晰易读。
前面我们看到了如何使用JDOM读取XML和如何使用JDOM编写XML。
其中我们将学习如何使用JDOM编辑XML文件。
JDOM编辑XML文件
对于本教程,我们有以下" employees.xml"文件。
<?xml version="1.0" encoding="UTF-8"?> <Employees xmlns="https://www.theitroad.local/employees"> <Employee id="1"> <age>25</age> <name>hyman</name> <gender>Male</gender> <role>Java Developer</role> </Employee> <Employee id="2"> <age>34</age> <name>Mona</name> <gender>Female</gender> <role>Manager</role> </Employee> <Employee id="3"> <age>45</age> <name>Dave</name> <gender>Male</gender> <role>Support</role> </Employee> </Employees>
我们想要更改XML中每个Employee元素的跟随。
更新所有名称元素以禁止使用字母。
在ID属性中,将M代表男性,将F代表女性。
删除性别元素
为每个员工添加默认值1000的新元素工资
JDOM示例–编辑XML文件
这是上述更改的Java程序。
package com.theitroad.xml.jdom;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class JDOMXMLEditor {
public static void main(String[] args) throws JDOMException, IOException {
final Namespace ns = Namespace.getNamespace("https://www.theitroad.local/employees");
//Get the JDOM document
org.jdom2.Document doc = useSAXParser("employees.xml");
//Get list of Employee element
Element rootElement = doc.getRootElement();
List<Element> listEmpElement = rootElement.getChildren("Employee", ns);
//loop through to edit every Employee element
for (Element empElement : listEmpElement) {
//change the name to BLOCK letters
String name = empElement.getChildText("name", ns);
if (name != null)
empElement.getChild("name", ns).setText(name.toUpperCase());
//edit the ID attribute based on Gender
String gender = empElement.getChildText("gender", ns);
if (gender != null && gender.equalsIgnoreCase("female")) {
String id = empElement.getAttributeValue("id");
empElement.getAttribute("id").setValue(id + "F");
} else {
String id = empElement.getAttributeValue("id");
empElement.getAttribute("id").setValue(id + "M");
}
//remove gender element as it's not needed anymore
empElement.removeChild("gender", ns);
//add salary element with default value to every employee
empElement.addContent(new Element("salary", ns).setText("1000"));
}
//document is processed and edited successfully, lets save it in new file
XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
//output xml to console for debugging
//xmlOutputter.output(doc, System.out);
xmlOutputter.output(doc, new FileOutputStream("employees_new.xml"));
}
//Get JDOM document from SAX Parser
private static org.jdom2.Document useSAXParser(String fileName) throws JDOMException,
IOException {
SAXBuilder saxBuilder = new SAXBuilder();
return saxBuilder.build(new File(fileName));
}
}
注意,使用命名空间来检索所有元素。
上面的程序产生以下输出xml。
employees_new.xml
<?xml version="1.0" encoding="UTF-8"?> <Employees xmlns="https://www.theitroad.local/employees"> <Employee id="1M"> <age>25</age> <name>hyman</name> <role>Java Developer</role> <salary>1000</salary> </Employee> <Employee id="2F"> <age>34</age> <name>MONA</name> <role>Manager</role> <salary>1000</salary> </Employee> <Employee id="3M"> <age>45</age> <name>DAVE</name> <role>Support</role> <salary>1000</salary> </Employee> </Employees>

