[size=large][b]先附上一个实体对象包含了 姓名 角色 年龄[/b][/size]
[size=x-large][b]利用JDOM来解析读取一份相对应的XML文件[/b][/size]
[size=large][color=red][b]employees.xml 文件内容如下[/b][/color][/size]
package com.gyj.jdom;
import java.io.Serializable;
/**
* 一个实体对象并且序列化
* @author GYJ
* @date 2014-3-22
*/
public class EmployeeObject implements Serializable{
private static final long serialVersionUID = -5538384024205531388L;
private String name;
private String role;
private int age;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the role
*/
public String getRole() {
return role;
}
/**
* @param role the role to set
*/
public void setRole(String role) {
this.role = role;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "EmployeeObject [name=" + name + ", role=" + role + ", age="
+ age + "]";
}
}
[size=x-large][b]利用JDOM来解析读取一份相对应的XML文件[/b][/size]
package com.gyj.jdom;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.DOMBuilder;
import org.xml.sax.SAXException;
/**
* 利用JDOM读取XML文件
* @author GYJ
* @date 2014-3-23
*/
public class JDOMXMLReader {
/**
* 得到JDOM的文档对象还有其他的方式列如:sax stax解析器都可以得到, 这里这介绍一种的使用就行了,也是性能比较好的JDOM
* <p>需要下载JAR包jdom-2.0.5.jar URL:http://www.jdom.org/dist/binary/
* @param args
*/
public static void main(String[] args) {
//这是一份xml文件
String fileName = "employees.xml";
//定义JDOM文档对象
try {
Document jdomDoc = useDOMParser(fileName);
//得到根节点
Element root = jdomDoc.getRootElement();
List<Element> listElements = root.getChildren("Employee");
List<EmployeeObject> list = new ArrayList<>();
for (Element empElement : listElements) {
EmployeeObject eobj = new EmployeeObject();
eobj.setAge(Integer.valueOf(empElement.getChildText("age")));
eobj.setName(empElement.getChildText("name"));
eobj.setRole(empElement.getChildText("role"));
list.add(eobj);
}
//输出结果
for (EmployeeObject empObj : list) {
System.out.println(empObj);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//out put=============
/*EmployeeObject [name=gyj, role=Java, age=29]
EmployeeObject [name=Lisa, role=CEO, age=35]
EmployeeObject [name=Tom, role=Manager, age=40]*/
}
/**
* 使用DOM解析器得到文档对象
* @param fileName
* @return
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
private static Document useDOMParser(String fileName) throws ParserConfigurationException, SAXException, IOException {
//创建文档对象
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
org.w3c.dom.Document doc = dBuilder.parse(new File(fileName));
//DOMBuilder 是JDOM的文档类
DOMBuilder domBuilder = new DOMBuilder();
//返回JDOM文档对象
return domBuilder.build(doc);
}
}
[size=large][color=red][b]employees.xml 文件内容如下[/b][/color][/size]
<?xml version="1.0" encoding="UTF-8"?>
<Employees>
<Employee id="1">
<age>29</age>
<name>gyj</name>
<role>Java</role>
</Employee>
<Employee id="2">
<age>35</age>
<name>Lisa</name>
<role>CEO</role>
</Employee>
<Employee id="3">
<age>40</age>
<name>Tom</name>
<role>Manager</role>
</Employee>
</Employees>