SAX基于事件的解析,解析器在一次读取XML文件中根据读取的数据产生相应的事件,由应用程序实现相应的事件处理逻辑,即它是一种“推”的解析方式;
这种解析方法速度快、占用内存少,但是它需要应用程序自己处理解析器的状态,实现起来会比较麻烦。
dom4j解析xml: http://www.cnblogs.com/gavinYang/p/3505535.html
jdom解析xml: http://www.cnblogs.com/gavinYang/p/3505530.html
dom解析: http://www.cnblogs.com/gavinYang/p/3505523.html
Java代码:
1 package com.test; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import javax.xml.parsers.SAXParser; 9 import javax.xml.parsers.SAXParserFactory; 10 11 import org.xml.sax.Attributes; 12 import org.xml.sax.SAXException; 13 import org.xml.sax.helpers.DefaultHandler; 14 15 public class SaxXML { 16 17 public static void main(String[] args) { 18 File file = new File("e:/People.xml"); 19 try { 20 SAXParserFactory spf = SAXParserFactory.newInstance(); 21 SAXParser parser = spf.newSAXParser(); 22 SaxHandler handler = new SaxHandler("People"); 23 parser.parse(new FileInputStream(file), handler); 24 25 List<People> peopleList = handler.getPeoples(); 26 for(People people : peopleList){ 27 System.out.println(people.getId()+" "+people.getName()+" "+people.getEnglishName()+" "+people.getAge()); 28 } 29 } catch (Exception e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 } 34 35 } 36 37 class SaxHandler extends DefaultHandler { 38 private List<People> peoples = null; 39 private People people; 40 private String currentTag = null; 41 private String currentValue = null; 42 private String nodeName = null; 43 44 public List<People> getPeoples() { 45 return peoples; 46 } 47 48 public SaxHandler(String nodeName) { 49 this.nodeName = nodeName; 50 } 51 52 @Override 53 public void startDocument() throws SAXException { 54 // TODO 当读到一个开始标签的时候,会触发这个方法 55 super.startDocument(); 56 57 peoples = new ArrayList<People>(); 58 } 59 60 @Override 61 public void endDocument() throws SAXException { 62 // TODO 自动生成的方法存根 63 super.endDocument(); 64 } 65 66 @Override 67 public void startElement(String uri, String localName, String name, 68 Attributes attributes) throws SAXException { 69 // TODO 当遇到文档的开头的时候,调用这个方法 70 super.startElement(uri, localName, name, attributes); 71 72 if (name.equals(nodeName)) { 73 people = new People(); 74 } 75 if (attributes != null && people != null) { 76 for (int i = 0; i < attributes.getLength(); i++) { 77 if(attributes.getQName(i).equals("id")){ 78 people.setId(attributes.getValue(i)); 79 } 80 else if(attributes.getQName(i).equals("en")){ 81 people.setEnglishName(attributes.getValue(i)); 82 } 83 } 84 } 85 currentTag = name; 86 } 87 88 @Override 89 public void characters(char[] ch, int start, int length) 90 throws SAXException { 91 // TODO 这个方法用来处理在XML文件中读到的内容 92 super.characters(ch, start, length); 93 94 if (currentTag != null && people != null) { 95 currentValue = new String(ch, start, length); 96 if (currentValue != null && !currentValue.trim().equals("") && !currentValue.trim().equals(" ")) { 97 if(currentTag.equals("Name")){ 98 people.setName(currentValue); 99 } 100 else if(currentTag.equals("Age")){ 101 people.setAge(currentValue); 102 } 103 } 104 } 105 currentTag = null; 106 currentValue = null; 107 } 108 109 @Override 110 public void endElement(String uri, String localName, String name) 111 throws SAXException { 112 // TODO 在遇到结束标签的时候,调用这个方法 113 super.endElement(uri, localName, name); 114 115 if (name.equals(nodeName)) { 116 peoples.add(people); 117 } 118 } 119 120 }
People对象:
1 package com.test; 2 3 public class People { 4 private String id; 5 private String name; 6 private String englishName; 7 private String age; 8 public String getId() { 9 return id; 10 } 11 public void setId(String id) { 12 this.id = id; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public String getEnglishName() { 21 return englishName; 22 } 23 public void setEnglishName(String englishName) { 24 this.englishName = englishName; 25 } 26 public String getAge() { 27 return age; 28 } 29 public void setAge(String age) { 30 this.age = age; 31 } 32 33 }
xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <PeopleList> 3 <People id="1"> 4 <Name en='zhangsan'>张三</Name> 5 <Age>20</Age> 6 </People> 7 <People id="2"> 8 <Name en='lisi'>李四</Name> 9 <Age>30</Age> 10 </People> 11 </PeopleList>
原文出自:http://blog.csdn.net/zyting_love/article/details/6929307