DOM4j读取XML文件(SAXReader)
一、 总结: Document document=new SAXReader.reader(“xml文路径/文件名xxx.xml”);//得到Document对象 Element root = document.getRootElement()//获得根节点 Iterator iterator=root.elementIterator(); //从根节点遍历子节点 Iterator iterator=Element.elementIterator(); //再从子节点在遍历其子节点 对节点访问其属性用:Attribute leaderAttr =Element. attribute(“xxx”); 对节点访问其某个属性leaderAttr的名称:leaderAttr.getName(); 对节点访问其名称:Element.getName(); 对节点访问其文本:Element. getText();
简单代码为如下: Document document=new SAXReader.reader(“xml文路径/文件名xxx.xml”); HashMap hm = new HashMap<String,String>(); 。。。。。 for ( Iterator iterInner = element.elementIterator(); iterInner.hasNext(); ) { Element elementInner = (Element) iterInner.next();
//遍历college节点开始 if(elementInner.getName().equals("college")){ /添加college的文本 hm.put(elementInner.getName()+ num, elementInner.getText()); //添加college的leader属性 Attribute leaderAttr=elementInner.attribute("leader"); 。。。。。如果leaderAttr不是空字符或者其值不为null。。。。。 hm.put(elementInner.getName() + "-"+leaderAttr.getName()+ num, leaderAttr.getValue()); } else{ //遍历非college节点的其它节点 hm.put(elementInner.getName()+ num, elementInner.getText()); }
<?xml version="1.0" encoding="UTF-8"?>
<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>
package test2;
import java.io.File; import java.util.HashMap; import java.util.Iterator; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Dom4jReadExmple { public void 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()); } } }
} catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package dom4jExample.read; import java.util.HashMap; public class TestDom4jReadExmple { public static void main(String[] args) { try{ //获取解析完后的解析信息 HashMap<String,String> hashMap; Dom4jReadExmple drb=new Dom4jReadExmple(); //遍历整个XML文件 hashMap = new HashMap<String,String>(); drb.iterateWholeXML("studentInfo.xml", hashMap); System.out.println("姓名\t年龄\t学院\t学院领导\t电话\t\t备注"); for(int i=0;i<hashMap.size();i+=6){ int j=i/6; System.out.print(hashMap.get("name"+j)+"\t"); System.out.print(hashMap.get("student-age"+j)+"\t"); System.out.print(hashMap.get("college"+j)+"\t"); System.out.print(hashMap.get("college-leader"+j)+"\t"); System.out.print(hashMap.get("telephone"+j)+"\t"); System.out.println(hashMap.get("notes"+j)+"\t"); } }catch(Exception ex){ ex.printStackTrace(); } } }
二、 一个读写操作的例子:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Iterator;
import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter;
public class testXml {
public static void main(String[] args) {
try {
testXml test = new testXml(); test.testWrite(); test.testRead();
} catch (Exception e) { e.printStackTrace(); }
}
public void testRead() throws Exception{
SAXReader reader = new SAXReader(); Document document = reader.read(new FileInputStream("src/output.xml"));
Element root = document.getRootElement();
for (Iterator iter = root.elementIterator(); iter.hasNext();) { Element element = (Element) iter.next(); System.out.println(element.getText()); System.out.println(element.attributeValue("name")); System.out.println(element.attributeValue("blog")); }
}
public void testWrite() throws Exception{
Document document = DocumentHelper.createDocument(); Element root = document.addElement("root"); for(int i=0;i<10;i++){
Element element1 = root.addElement("user") .addAttribute("name","Alex"+i) .addAttribute("id", "id"+i) .addText("我是信息"); }
XMLWriter writer = new XMLWriter(new FileOutputStream("src/output.xml"));
writer.write(document); writer.close();
}
} 输出的效果: <?xml version="1.0" encoding="UTF-8"?> <root> <user name="Alex0" id="id0">我是信息</user> <user name="Alex1" id="id1">我是信息</user> <user name="Alex2" id="id2">我是信息</user> <user name="Alex3" id="id3">我是信息</user> <user name="Alex4" id="id4">我是信息</user> <user name="Alex5" id="id5">我是信息</user> <user name="Alex6" id="id6">我是信息</user> <user name="Alex7" id="id7">我是信息</user> <user name="Alex8" id="id8">我是信息</user> <user name="Alex9" id="id9">我是信息</user> </root> |