SAX(Simple API for XML)解析:基于事件流的解析。
特点:速度快,文档大小无限制
只读,不能随机访问,不好查找节点
java包:org.xml.sax/org.xml.sax.helpers
常用接口/类:
XMLReader
ContentHandler
DTDHandler
EntityResolver
ErrorHandler
DefaultHandler
DefaultHandler事件举例:
startDocument():
startElement(String uri, String localName, String qName,Attributes attributes)
characters(char[] ch, int start, int length)
endElement(String uri, String localName, String qName)
编程步骤:
1.准备一个xml文件,和DOM解析一样,movies.xml:
<?xml version="1.0" encoding="UTF-8"?> <Movies> <Movie id="1"> <name>愤怒的小鸟</name> <type>animation</type> <year>2016</year> </Movie> <Movie id="2"> <name>叶问</name> <type>action</type> <language>chinese</language> </Movie> </Movies>2.创建SAXParserFactory对象
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser saxParser=spf.newSAXParser();
saxParser.parse("movies.xml", new MyHandler());
这个类继承了DefaultHandler,下面开始讲解步骤:
(1)开始xml解析:
@Override public void startDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("开始解析"); movielist=new ArrayList<Movie>(); }
(2)开始解析每一部电影:
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub if(qName.equals("Movie")){ movie=new Movie(); count++; System.out.println("正在解析第"+count+"部电影"); for(int i=0;i<attributes.getLength();i++){ System.out.println("属性名:"+attributes.getQName(i)); System.out.println("属性值:"+attributes.getValue(i)); if(attributes.getQName(i).equals("id")){ movie.setId(attributes.getValue(i)); } } } }(3)开始解析每一部电影的节点值:
@Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub value=new String(ch,start,length); if(!value.trim().equals("")){//去除空格之后不是空字符串 System.out.println("节点值:"+value); } }
@Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub if(qName.equals("Movie")){ movielist.add(movie); movie=null; System.out.println("第"+count+"部电影解析结束"); } if(qName.equals("year")){ movie.setYear(value); }else if (qName.equals("name")) { movie.setName(value); }else if (qName.equals("language")) { movie.setLanguage(value); }else if (qName.equals("type")) { movie.setYear(value); } }
@Override public void endDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("结束解析"); }
public class MyHandler extends DefaultHandler{ private String value; private int count=0; private Movie movie; private ArrayList<Movie>movielist; @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("开始解析"); movielist=new ArrayList<Movie>(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub if(qName.equals("Movie")){ movie=new Movie(); count++; System.out.println("正在解析第"+count+"部电影"); for(int i=0;i<attributes.getLength();i++){ System.out.println("属性名:"+attributes.getQName(i)); System.out.println("属性值:"+attributes.getValue(i)); if(attributes.getQName(i).equals("id")){ movie.setId(attributes.getValue(i)); } } } } @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub value=new String(ch,start,length); if(!value.trim().equals("")){//去除空格之后不是空字符串 System.out.println("节点值:"+value); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub if(qName.equals("Movie")){ movielist.add(movie); movie=null; System.out.println("第"+count+"部电影解析结束"); } if(qName.equals("year")){ movie.setYear(value); }else if (qName.equals("name")) { movie.setName(value); }else if (qName.equals("language")) { movie.setLanguage(value); }else if (qName.equals("type")) { movie.setYear(value); } } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("结束解析"); } }
7.Sax:
public class Sax { public static void main(String[] args) { //1.创建SAXParserFactory对象 SAXParserFactory spf=SAXParserFactory.newInstance(); try { //2.创建SAXParser对象 SAXParser saxParser=spf.newSAXParser(); saxParser.parse("movies.xml", new MyHandler()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class Movie { private String id,name,type,language,year; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } @Override public String toString() { // TODO Auto-generated method stub return "id:"+id+" "+"name:"+name+" "+"type:"+type+" "+"language:"+language+" " +"year:"+year; } }