(三)SAX流机制解析器
1.基本的xml文档
<?xml version="1.0" encoding="UTF-8" ?> <persons> <person> <name>Jim</name> <age>45</age> </person> <person> <name>Ann</name> <age>36</age> </person> </persons>
2.封装值的封装类
package com.dasenlin.prasexml; /** * 封装类 * @author Administrator * */ public class Persons { private String name; private Integer age; public Persons() { super(); } public Persons(String name, Integer age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
3.解析的流程逻辑类
package com.dasenlin.prasexml; import java.io.IOException; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.SAXException; /** * 用SAX解析xml * @author Administrator * */ public class ParseXml { public static void main(String[] args) { //1.创建解析工厂 SAXParserFactory factory =SAXParserFactory.newInstance(); //2.获取解析器 try { SAXParser parse =factory.newSAXParser(); //3.加载文档Document注册处理器 //4.编写处理器 PersonHandler handler=new PersonHandler(); parse.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("com/dasenlin/prasexml/persons.xml"),handler); List <Persons> person = handler.getPersons(); for(Persons any:person){ System.out.println("name:"+any.getName()+"\tage:"+any.getAge()); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
4.手写处理类
package com.dasenlin.prasexml; import java.util.ArrayList; import java.util.List; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** * 编写的处理器 * @author Administrator * */ public class PersonHandler extends DefaultHandler { private List<Persons> persons; private Persons person; private String tag; public List<Persons> getPersons() { return persons; } public void setPersons(List<Persons> persons) { this.persons = persons; } @Override//开始文档 public void startDocument() throws SAXException { persons =new ArrayList<Persons>(); } @Override//开始元素 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(null!=qName){ tag=qName; } if(null!=qName && qName.equals("person")){ person = new Persons(); } } @Override//String public void characters(char[] ch, int start, int length) throws SAXException { String str = new String(ch,start,length); if(null!=tag&&tag.equals("name")){ person.setName(str); }else if(null!=tag&&tag.equals("age")){ Integer age = Integer.parseInt(str); person.setAge(age); } } @Override//结束元素 public void endElement(String uri, String localName, String qName) throws SAXException { if(qName.equals("person")){ this.persons.add(person); } tag=null; } @Override//结束文档 public void endDocument() throws SAXException { // TODO 自动生成的方法存根 super.endDocument(); } }