附件是由郑州蜂鸟科技有限公司周源整理的java对xml的处理解决方案,具体介绍了dom4j及jdom的使用,感觉介绍的异常详细,欢迎大家下载使用。
注意:书中DOM4JParser.java中的readXMLInList()及readXMLInIterator()方法在遍历xml文档内容是有误,我已经做了改正,以下为修改后的代码:
/**
* 使用List方式遍历XML文档
*
* @param url
* XML文件路径
* @return 返回Company对象
* @throws DocumentException
* 使用SAXReader读取XML文档时需要处理该异常
*/
@SuppressWarnings("unchecked")
public Company readXMLInList(String url) throws DocumentException {
// 创建公司对象
Company comp = new Company();
// 使用org.dom4j.io.包下DOM4J已经配置好的SAXReader读取XML文档。
SAXReader reader = new SAXReader();
reader.setEncoding("utf-8");// 设置读取文件的编码为UTF-8
Document doc = reader.read(url); // 读取url指定的xml文件
// 得到文档的根元素company
Element root = doc.getRootElement();
// 使用Element的attributeValue(String arg)方法读取XML文档中的公司信息,并为公司对象设置相关属性。
comp.setAddress(root.attributeValue("address"));
comp.setName(root.attributeValue("name"));
// 通过elements(String arg)方法读取XML文档中的所有root节点下的department节点并返回一个List。
List<Element> depts = root.elements("department");
// 创建Department对象数组
Department[] deptList = new Department[depts.size()];
// 读取部门信息,并将部门对象添加至公司对象中。
for (int i = 0; i < depts.size(); i++) {
Department dept = new Department();
// 使用List的get(int index)方法遍历List,返回列表中的每一项。
Element deptEmt = depts.get(i);
dept.setName(deptEmt.attributeValue("name"));
dept.setNumber(deptEmt.attributeValue("deptNo"));
// 通过Element的elements("nodeName")方法获得子节点并返回一个java标准列表对象
List<Element> emps = deptEmt.elements("employee");
// 根据得到的列表大小创建Employee对象数组
Employee[] empList = new Employee[emps.size()];
/*
* 读取雇员信息,并将雇员对象添加至部门对象中。
*/
for (int j = 0; j < emps.size(); j++) {
Employee emp = new Employee();
// 将XML中的employee节点转换为Employee对象emp,并设置对象属性。
Element empEmt = emps.get(j);
emp.setName(empEmt.getText());
emp.setPosition(empEmt.attributeValue("position"));
emp.setTelephone(empEmt.attributeValue("telephone"));
// 将emp对象添加至Employee对象数组empList中
empList[j] = emp;
}
// 将对象数组empList添加至部门对象dept的对象数组属性emp中。
dept.setEmp(empList);
// 将dept对象添加至Department对象数组deptList中。
deptList[i] = dept;
}
/*
* 将对象数组deptList添加至公司对象comp的对象数组属性deptList中, 并返回该公司对象。
*/
comp.setDept(deptList);
return comp;
}
/**
* 使用Iterator方式遍历XML文档
*
* @param url
* XML文件路径
* @return 返回Company对象
* @throws DocumentException
* 使用SAXReader读取XML文档时需要处理该异常
*/
@SuppressWarnings("unchecked")
public Company readXMLInIterator(String url) throws DocumentException {
// 创建公司对象
Company comp = new Company();
// 使用org.dom4j.io.包下DOM4J已经配置好的SAXReader读取XML文档。
SAXReader reader = new SAXReader();
reader.setEncoding("utf-8");// 设置读取文件的编码为UTF-8
Document doc = reader.read(url); // 读取url指定的xml文件
// 得到文档的根元素company
Element root = doc.getRootElement();
// 使用Element的attributeValue(String arg)方法读取XML文档中的公司信息,并为公司对象设置相关属性。
comp.setAddress(root.attributeValue("address"));
comp.setName(root.attributeValue("name"));
// 创建Department对象数组
Department[] deptList = new Department[(root.elements("department")).size()];
/*
* 本段代码解释:读取部门信息,并将部门对象添加至公司对象中。
* for循环语句解释:生成遍历root节点下名称为department的所有元素节点的迭代器
*/
for (Iterator deptIte = root.elementIterator("department"); deptIte.hasNext();) {
int i = 0, j = 0;// 数组下标标志值
Department dept = new Department();
// 使用List的get(int index)方法遍历List,返回集合中的第 每一项。
Element deptEmt = (Element) deptIte.next();
dept.setName(deptEmt.attributeValue("name"));
dept.setNumber(deptEmt.attributeValue("deptNo"));
// 创建Employee对象数组
Employee[] empList = new Employee[deptEmt.elements("employee").size()];
/*
* 本段代码:读取雇员信息,并将雇员对象添加至部门对象中。
* for循环语句:生成遍历deptEmt节点下名称为employee的所有元素节点的迭代器
*/
for (Iterator empIte = root.elementIterator("department"); deptIte.hasNext();) {
Employee emp = new Employee();
// 将XML中的employee节点转换为Employee对象emp。
Element empEmt = (Element) empIte.next();
emp.setName(empEmt.getText());
emp.setPosition(empEmt.attributeValue("position"));
emp.setTelephone(empEmt.attributeValue("telephone"));
// 将emp对象添加至Employee对象数组empList中
empList[j] = emp;
j++;
}
// 将对象数组empList添加至部门对象dept的对象数组属性emp中。
dept.setEmp(empList);
// 将dept对象添加至Department对象数组deptList中。
deptList[i] = dept;
i++;
}
/*
* 将对象数组deptList添加至公司对象comp的对象数组属性deptList中, 并返回该公司对象。
*/
comp.setDept(deptList);
return comp;
}
DOM4JParser.java中的XMLHandle()方法中的
写道
//1.修改company节点的address属性
company.attributeValue("address", "郑州市金水区文产路交叉口向东50米SOHO世纪城");
company.attributeValue("address", "郑州市金水区文产路交叉口向东50米SOHO世纪城");
应该为:
// 1.修改company节点的address属性
company.setAttributeValue("address", "郑州市金水区文产路交叉口向东50米SOHO世纪城");