Java代码如下:
package javaForXML;
imp
imp
imp
imp
imp
imp
imp
imp
imp
/**
* 利用dom4j进行XML编程
*
* @author cuiweibing
* @since 2007.8.10
*/
public class PraseXMLTest3 {
/**
* 遍历整个XML文件,获取所有节点的值与其属性的值,并放入HashMap中
*
* @param filename
* String 待遍历的XML文件(相对路径或者绝对路径)
* @param hm
* HashMap 存放遍历结果,格式:<nodename,nodevalue>或者<nodename+attrname,attrvalue>
*/
public Map<String,String> iterateWholeXML(String filename, HashMap<String, String> hm) {
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(new File(filename));
Element root = document.getRootElement();
// 用于记录学生编号的变量
int num = -1;
// 遍历根结点(students)的所有孩子节点(肯定是student节点)
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
num++;
// 获取person节点的age属性的值
Attribute ageAttr = element.attribute("age");
if (ageAttr != null) {
String age = ageAttr.getValue();
if (age != null && !age.equals("")) {
hm.put(element.getName() + "-" + ageAttr.getName()+ num, age);
} else {
hm.put(element.getName() + "-" + ageAttr.getName()+ num, "20");
}
} else {
hm.put(element.getName() + "-age" + num, "20");
}
// 遍历student结点的所有孩子节点(即name,college,telphone,notes),并进行处理
for (Iterator iterInner = element.elementIterator(); iterInner
.hasNext();) {
Element elementInner = (Element) iterInner.next();
if (elementInner.getName().equals("college")) {
hm.put(elementInner.getName() + num, elementInner.getText());
// 获取college节点的leader属性的值
Attribute leaderAttr = elementInner.attribute("leader");
if (leaderAttr != null) {
String leader = leaderAttr.getValue();
if (leader != null && !leader.equals("")) {
hm.put(elementInner.getName() + "-"+ leaderAttr.getName() + num, leader);
} else {
hm.put(elementInner.getName() + "-"+ leaderAttr.getName() + num, "leader");
}
} else {
hm.put(elementInner.getName() + "-leader" + num,"leader");
}
} else {
hm.put(elementInner.getName() + num, elementInner.getText());
}
}
}
return hm;
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return hm;
}
public static void main(String[] args) throws DocumentException{
String filename="d:\zhang.xml";
HashMap <String,String>map = new HashMap<String, String>();
HashMap <String,String>map1 = (HashMap<String, String>) new PraseXMLTest3().iterateWholeXML(filename, map);
for(String ss:map1.keySet()){
System.out.println(ss+":"+map1.get(ss));
}
}
}
所解析的xml文件如下:
<?xml version="1.0" encoding="gb2312"?>
<students>
<student age="25"><!--如果没有age属性,默认的为20-->
<name>崔卫兵</name>
<college>PC学院</college>
<telephone>62354666</telephone>
<notes>男,1982年生,硕士,现就读于北京邮电大学</notes>
</student>
<student>
<name>cwb</name>
<college leader="学院领导">PC学院</college><!--如果没有leader属性,默认的为leader-->
<telephone>62358888</telephone>
<notes>男,1987年生,硕士,现就读于中国农业大学</notes>
</student>
<student age="45">
<name>xxxxx</name>
<college leader="">xxx学院</college>
<telephone>66666666</telephone>
<notes>注视中,注释中</notes>
</student>
<student age="">
<name>lxx</name>
<college>yyyy学院</college>
<telephone>88888888</telephone>
<notes>注视中111,注释中222</notes>
</student>
</students>