SAX方式解析XML
阿新 • • 發佈:2018-12-13
SAX方式解析XML原理
SAX的工作方式簡單地說就是對文件進行順序掃描,當掃描到文件(document)開始與結束,元素(Element)開始與結束、文件(document)結束等地方時通知事件處理函式,由事件處理函式做相應的動作,然後繼續同樣的掃描,直至文件結束。
優點:消耗資源比較少;適合大檔案解析;
缺點:只能讀取不能修改;開發複雜;
SAX方式解析XML示例
1 <?xml version="1.0" encoding="UTF-8"?> 2 <student id="001"> 3 <name>張三</name> 4 <sex>男</sex> 5 <age>20</age> 6 </student>
SAX01解析:
1 package com.java1234.xml; 2 3 import javax.xml.parsers.ParserConfigurationException; 4 import javax.xml.parsers.SAXParser; 5 import javax.xml.parsers.SAXParserFactory; 6 7 import org.xml.sax.Attributes; 8 import org.xml.sax.SAXException;9 import org.xml.sax.helpers.DefaultHandler; 10 11 public class SAX01 extends DefaultHandler{ 12 13 @Override 14 public void startDocument() throws SAXException { 15 // TODO Auto-generated method stub 16 System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 17} 18 19 @Override 20 public void endDocument() throws SAXException { 21 // TODO Auto-generated method stub 22 System.out.println("文件載入完畢!"); 23 } 24 25 @Override 26 public void startElement(String uri, String localName, String qName, 27 Attributes attributes) throws SAXException { 28 // TODO Auto-generated method stub 29 System.out.println("元素節點開始載入"+qName); 30 } 31 32 @Override 33 public void endElement(String uri, String localName, 34 String qName) throws SAXException { 35 // TODO Auto-generated method stub 36 System.out.println("元素節點結束載入"+qName); 37 } 38 39 @Override 40 public void characters(char[] ch, int start, int length) throws SAXException { 41 // TODO Auto-generated method stub 42 System.out.println("掃描文字節點"+new String(ch,start,length)); 43 } 44 45 public static void main(String[] args) throws Exception { 46 SAXParserFactory factory=SAXParserFactory.newInstance(); 47 SAXParser parser=factory.newSAXParser(); 48 parser.parse("src/demo01.xml", new SAX01()); 49 } 50 }
解析更加複雜的資訊;例如形成一個類重新輸出類裡面的數值啥的!
Student類
1 package com.java1234.model; 2 3 public class Student { 4 private String name; 5 private String sex; 6 private String id; 7 private int age; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getSex() { 15 return sex; 16 } 17 public void setSex(String sex) { 18 this.sex = sex; 19 } 20 public String getId() { 21 return id; 22 } 23 public void setId(String id) { 24 this.id = id; 25 } 26 public int getAge() { 27 return age; 28 } 29 public void setAge(int age) { 30 this.age = age; 31 } 32 @Override 33 public String toString() { 34 // TODO Auto-generated method stub 35 return this.id+","+this.name+","+this.age+","+this.sex;//重寫toString 方法 36 } 37 38 }
XML如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <students> 3 <student id="001"> 4 <name>張三</name> 5 <sex>男</sex> 6 <age>20</age> 7 </student> 8 <student id="002"> 9 <name>李四</name> 10 <sex>女</sex> 11 <age>18</age> 12 </student> 13 </students>
1 package com.java1234.xml; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import javax.xml.parsers.ParserConfigurationException; 7 import javax.xml.parsers.SAXParser; 8 import javax.xml.parsers.SAXParserFactory; 9 import org.xml.sax.Attributes; 10 import org.xml.sax.SAXException; 11 import org.xml.sax.helpers.DefaultHandler; 12 13 import com.java1234.model.Student; 14 15 public class SAX3 extends DefaultHandler{ 16 private List<Student>students=null; 17 private Student student=null; 18 private String tag=null;//標記上一個節點 19 20 @Override 21 public void startDocument() throws SAXException { 22 // TODO Auto-generated method stub 23 System.out.println("開始讀取學生資訊"); 24 students=new ArrayList<Student>(); 25 } 26 27 @Override 28 public void endDocument() throws SAXException { 29 // TODO Auto-generated method stub 30 System.out.println("\n 學生資訊讀取完畢"); 31 } 32 33 @Override 34 public void startElement(String uri, String localName, String qName, 35 Attributes attributes) throws SAXException { 36 // TODO Auto-generated method stub 37 //attributes 屬性節點操作 38 if("student".equals(qName)){ 39 student=new Student(); 40 student.setId(attributes.getValue(0)); 41 } 42 tag=qName; 43 } 44 45 @Override 46 public void endElement(String uri, String localName, 47 String qName) throws SAXException { 48 // TODO Auto-generated method stub 49 if("student".equals(qName)){ 50 students.add(student); 51 student=null; 52 } 53 tag=null; 54 } 55 56 @Override 57 public void characters(char[] ch, int start, int length) throws SAXException { 58 // TODO Auto-generated method stub 59 if(tag!=null){ 60 String content=new String(ch,start,length); 61 if("name".equals(tag)){ 62 student.setName(content); 63 }else if("age".equals(tag)){ 64 student.setAge(Integer.parseInt(content)); 65 }else if("sex".equals(tag)){ 66 student.setSex(content); 67 } 68 } 69 } 70 71 public static void main(String[] args) throws Exception { 72 SAXParserFactory factory=SAXParserFactory.newInstance(); 73 SAXParser parser=factory.newSAXParser(); 74 SAX3 sax3=new SAX3(); 75 parser.parse("src/students.xml", sax3); 76 for(Student s:sax3.students){ 77 System.out.println(s); 78 } 79 } 80 81 82 83 }
結果如下所示: