既然前面说了DOM4J这里好那里好,大家都是在用这个,那咱就不得不写一个了。
XML文件:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <person> 3 <from where="Others"> 4 <name>Jack</name> 5 <age>25</age> 6 <sex>boy</sex> 7 <hobby>sing songs</hobby> 8 </from> 9 <from where="China"> 10 <name>桔子桑</name> 11 <age>26</age> 12 <sex>男</sex> 13 <hobby>看书、画画</hobby> 14 </from> 15 </person>
Person类,用于存储解析所得信息:
1 package com.dom.node; 2 3 public class Person { 4 String where; 5 String name; 6 String age; 7 String sex; 8 String hobby; 9 public void setWhere(String where) { 10 this.where = where; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public void setAge(String age) { 16 this.age = age; 17 } 18 public void setSex(String sex) { 19 this.sex = sex; 20 } 21 public void setHobby(String hobby) { 22 this.hobby = hobby; 23 } 24 @Override 25 public String toString() { 26 return "Person [where=" + where + ", name=" + name + ", age=" + age + ", sex=" + sex + ", hobby=" + hobby + "]"; 27 } 28 }
DOM4J解析方法类:
1 package com.dom.node; 2 3 import java.util.*; 4 import org.dom4j.Attribute; 5 import org.dom4j.Document; 6 import org.dom4j.Element; 7 import org.dom4j.io.SAXReader; 8 9 public class Dom4jxml { 10 public static void main(String[] args) { 11 try { 12 SAXReader reader = new SAXReader(); 13 Document document = reader.read("file/clear.xml"); 14 Element rootElement = document.getRootElement(); 15 Iterator<Element> it =rootElement.elementIterator(); 16 List<Person> personlist = new ArrayList<Person>(); 17 while(it.hasNext()) { 18 Person person = new Person(); 19 Element son1=(Element)it.next(); 20 List<Attribute> attrlist1 =son1.attributes(); 21 for (Attribute attr : attrlist1) { 22 if(attr.getName().equals("where")) { 23 person.setWhere(attr.getValue()); 24 } 25 } 26 Iterator itt = son1.elementIterator(); 27 while(itt.hasNext()) { 28 Element son2 =(Element)itt.next(); 29 if(son2.getName().equals("name")) { 30 person.setName(son2.getStringValue()); 31 }else if(son2.getName().equals("age")) { 32 person.setAge(son2.getStringValue()); 33 }else if(son2.getName().equals("sex")) { 34 person.setSex(son2.getStringValue()); 35 }else if(son2.getName().equals("hobby")) { 36 person.setHobby(son2.getStringValue()); 37 } 38 } 39 personlist.add(person); 40 person=null; 41 } 42 System.out.println("总人数:"+personlist.size()+"人"); 43 for (Person person : personlist) { 44 System.out.println(person.toString()); 45 } 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 }
控制台打印输出:
总人数:2人
Person [where=Others, name=Jack, age=25, sex=boy, hobby=sing songs]
Person [where=China, name=桔子桑, age=26, sex=男, hobby=看书、画画]
总结一下其中获取节点相关数据的方法吧,以免混淆;
SAXReader reader = new SAXReader(); //新建SAXReader对象
Document document = reader.read("file/clear.xml"); //read()方法将xml文件载入当前程序
Element rootElement = document.getRootElement(); //getRootElement()方法获取文档的根节点
Iterator<Element> it =rootElement.elementIterator(); //elementIterator()返回一个迭代器
attributes() ; //返回节点的属性(List集合)
getName(); //得到属性名或者节点名
getValue(); //得到属性值
getStringValue(); //得到节点内部的字符串