SAX方式解析XML
Posted zyxsblogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SAX方式解析XML相关的知识,希望对你有一定的参考价值。
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(" 学生信息读取完毕"); 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 }
结果如下所示:
以上是关于SAX方式解析XML的主要内容,如果未能解决你的问题,请参考以下文章