XML读写之JDOM
本文先使用JDOM方式写一个简单地小例子,亲测可以运行,然后在末尾分析其优缺点。 1.准备
<?xml version="1.0" encoding="UTF-8"?>
<root>
<student id="1">
<name>张三</name>
<age>18</age>
<gender>male</gender>
</student>
<student id="2">
<name>李四</name>
<age>19</age>
<gender>male</gender>
</student>
<student id="3">
<name>王五</name>
<age>21</age>
<gender>female</gender>
</student>
</root>
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
2.编码
public class StudentBean {
private String id;
private String name;
private String gender;
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "StudentBean [id=" + id + ",name=" + name + ",gender=" + gender + ",age=" + age + "]";
}
}
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import bean.StudentBean;
public class JDOMTest {
public static List<StudentBean> studentsList = new ArrayList<StudentBean>();
public static void main(String[] args) {
//遍历XML文件中的对象
List<StudentBean> list = visitXML();
Iterator<StudentBean> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
updateXML("src/main/resources/student.xml");
writeXMLFile("src/main/resources/jdom.xml");
}
public static List<StudentBean> visitXML() {
try {
SAXBuilder builder = new SAXBuilder(false);
Document document = builder.build("src/main/resources/student.xml");
//获取根元素
Element root = document.getRootElement();
List<Element> students = root.getChildren("student");
System.out.println("XML文档中共有"+students.size()+"个学生对象!");
for(Iterator<Element> it = students.iterator();it.hasNext();) {
Element student = it.next();
StudentBean stu = new StudentBean();
stu.setId(student.getAttributeValue("id"));
stu.setName(student.getChild("name").getText());
stu.setAge(Integer.parseInt(student.getChildText("age")));
stu.setGender(student.getChildText("gender"));
studentsList.add(stu);
stu = null;
}
} catch (Exception e) {
e.printStackTrace();
}
return studentsList;
}
public static void updateXML(String fileName) {
try {
SAXBuilder builder = new SAXBuilder(false);
Document document = builder.build(fileName);
//获取根元素
Element root = document.getRootElement();
List<Element> students = root.getChildren("student");
//充值元素值
students.get(1).getChild("name").setText("mason");
//移除元素
students.get(0).removeChild("gender");
//新增元素
Element phone = new Element("phone");
phone.setText("15555555");
students.get(0).addContent(phone);
XMLOutputter outputter=new XMLOutputter();
outputter.output(document,new FileOutputStream(fileName));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("XML文档更改成功!");
}
public static void writeXMLFile(String fileName) {
//创建根节点
Element root = new Element("fruit");
//添加第一个元素apple
Element apple = new Element("apple");
Element color = new Element("color");
color.setText("red");
Element price = new Element("price");
price.setText("12.2");
apple.addContent(color);
apple.addContent(price);
root.addContent(apple);
//添加第二个元素apple
Element apple2 = new Element("apple");
Element color2 = new Element("color");
color2.setText("green");
Element price2 = new Element("price");
price2.setText("10.88");
apple2.addContent(color2);
apple2.addContent(price2);
root.addContent(apple2);
//添加第三个元素orange
Element orange = new Element("orange");
Element color3 = new Element("color");
color3.setText("yellow");
Element price3 = new Element("price");
price3.setText("3.33");
orange.addContent(color3);
orange.addContent(price3);
root.addContent(orange);
//写入到新的XML文件中
try {
//将根节点添加到文档中
Document document = new Document(root);
XMLOutputter XMLOut = new XMLOutputter(FormatXML());
XMLOut.output(document,new FileOutputStream("src/main/resources/jdom.xml"));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("XML文档写入成功!");
}
public static Format FormatXML(){
//格式化生成的xml文件,如果不进行格式化的话,生成的xml文件将会是很长的一行...
Format format = Format.getCompactFormat();
format.setEncoding("utf-8");
format.setIndent(" ");
return format;
}
}
3.运行结果程序修改后的student.xml为: <?xml version="1.0" encoding="UTF-8"?>
<root>
<student id="1">
<name>张三</name>
<age>18</age>
<phone>15555555</phone></student>
<student id="2">
<name>mason</name>
<age>19</age>
<gender>male</gender>
</student>
<student id="3">
<name>王五</name>
<age>21</age>
<gender>female</gender>
</student>
</root>
程序自行新建的XML文件jdom.xml为: <?xml version="1.0" encoding="utf-8"?>
<fruit>
<apple>
<color>red</color>
<price>12.2</price>
</apple>
<apple>
<color>green</color>
<price>10.88</price>
</apple>
<orange>
<color>yellow</color>
<price>3.33</price>
</orange>
</fruit>
4.结束 优点: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |