XML:可扩展标记语言。 xml可以当成一个小型的数据库, html / html5 xhtml(格式比较严格) | xml xml解析方式有2种: -SAX,SAX是基于事件流的解析,事件流解析就是在xml中一行一行的解析不会后头。 -DOM解析,把整个xml这棵树加载到内存里面,需要用哪部分就拿哪部分。dOM解析适合数据量比较小的,数据量比较大就会占内存。 常见的四种解析方式: 1.DOM生成和解析XML文档 2.SAX生成和解析XML文档 3.DOM4J(一种框架和类库)生成和解析XML文档 4.JDOM生成和解析XML SAX解析步骤: 1.获得解析工厂 SAXParserFactory 2.工厂获取解析器SAXParser 3.加载文档Document 注册处理器 解析person.xml文件: <?xml version="1.0" encoding="UTF-8" ?><!--声明部分--><!-- 正文,一个xml只有一个根--> <persons> <person> <name>至尊宝</name> <age>9000</age> </person> <person> <name>白晶晶</name> <age>7000</age> </person> </persons> //对xml进行解析,解析时用到Person类 public class Person { private String name; private int age; public Person() { } public Person(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } public class ParseDemo01 {//SAX解析XML public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { //1、获取解析工厂 /* protected SAXParserFactory () { } 构造器是protected,所以不能new */ SAXParserFactory factory=SAXParserFactory.newInstance(); //2、从解析工厂获取解析器 SAXParser parse =factory.newSAXParser(); //3、加载文档 Document,注册处理器(指定处理器) //编写处理器 PersonHandler handler=new PersonHandler(); parse.parse(Thread.currentThread().getContextClassLoader() .getResourceAsStream("com/bjsxt/xml/person.xml") ,handler ); List<Person> persons =handler.getPersons(); for(Person p:persons){ System.out.println(p.getName()+"-->"+p.getAge()); } } } 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<Person> persons; private Person person; private String tag;//记录标签名 @Override public void startDocument() throws SAXException { System.out.println("处理文档开始"); persons =new ArrayList<Person>(); } @Override public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException { System.out.println("开始一个元素" +qName); if(null!=qName){ tag=qName; } if(null!=qName &&qName.equals("person")){ person =new Person(); } } @Override public void characters(char[] ch, int start, int length) throws SAXException { String str =new String(ch,start,length); if(null!=tag &&tag.equals("name")){ System.out.println(new String(ch,start,length)); person.setName(str); }else if(null!=tag &&tag.equals("age")){ Integer age = Integer.valueOf(str); person.setAge(age); } } @Override public void endElement(String uri, String localName, String qName)throws SAXException { System.out.println("结束一个元素" +qName); if(qName.equals("person")){ this.persons.add(person); } tag =null; } @Override public void endDocument() throws SAXException { System.out.println("文档处理结束"); } public List<Person> getPersons() { return persons; } public void setPersons(List<Person> persons) { this.persons = persons; } } /*处理文档开始 开始一个元素persons 开始一个元素person 开始一个元素name 至尊宝 结束一个元素name 开始一个元素age 结束一个元素age 结束一个元素person 开始一个元素person 开始一个元素name 白晶晶 结束一个元素name 开始一个元素age 结束一个元素age 结束一个元素person 结束一个元素persons 文档处理结束 至尊宝-->9000 白晶晶-->7000*/ 解析web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>login</servlet-name> <servlet-class>com.bjsxt.server.demo4.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> <url-pattern>/g</url-pattern> </servlet-mapping> </web-app> //SAX解析 public class WebHandler extends DefaultHandler{ private List<Entity> entityList; private List<Mapping> mappingList; private Entity entity; private Mapping mapping; private String beginTag ; private boolean isMap; @Override public void startDocument() throws SAXException { //文档解析开始 entityList =new ArrayList<Entity>() ; mappingList =new ArrayList<Mapping>() ; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { //开始元素 if(null!=qName){ beginTag=qName;//不同的方法中都要用到的变量就声明为全局变量即成员属性。 if(qName.equals("servlet")){ isMap=false; entity=new Entity(); }else if(qName.equals("servlet-mapping")){ isMap=true; mapping=new Mapping(); } } } @Override public void characters(char[] ch, int start, int length) throws SAXException { /* ch数组每次的值都是一样的 [ , , <, w, e, b, -, a, p, p, >, , , , <, s, e, r, v, l, e, t, >, , , , , <, s, e, r, v, l, e, t, -, n, a, m, e, >, l, o, g, i, n, <, /, s, e, r, v, l, e, t, -, n, a, m, e, >, , , , , <, s, e, r, v, l, e, t, -, c, l, a, s, s, >, c, o, m, ., b, j, s, x, t, ., s, e, r, v, e, r, ., d, e, m, o, 4, ., L, o, g, i, n, S, e, r, v, l, e, t, <, /, s, e, r, v, l, e, t, -, c, l, a, s, s, >, , , , <, /, s, e, r, v, l, e, t, >, , , , <, s, e, r, v, l, e, t, -, m, a, p, p, i, n, g, >, , , , , <, s, e, r, v, l, e, t, -, n, a, m, e, >, l, o, g, i, n, <, /, s, e, r, v, l, e, t, -, n, a, m, e, >, , , , , <, u, r, l, -, p, a, t, t, e, r, n, >, /, l, o, g, i, n, <, /, u, r, l, -, p, a, t, t, e, r, n, >, , , , , <, u, r, l, -, p, a, t, t, e, r, n, >, /, g, <, /, u, r, l, -, p, a, t, t, e, r, n, >, , , , <, /, s, e, r, v, l, e, t, -, m, a, p, p, i, n, g, >, , , <, /, w, e, b, -, a, p, p, >, */ //处理内容 if(null!=beginTag){ String str =new String(ch,start,length); if(isMap ){ if(beginTag.equals("servlet-name")){ mapping.setName(str); }else if(beginTag.equals("url-pattern")){ mapping.getUrlPattern().add(str); } }else{ if(beginTag.equals("servlet-name")){ entity.setName(str); }else if(beginTag.equals("servlet-class")){ entity.setClz(str); } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { //结束元素 if(null!=qName){ if(qName.equals("servlet")){ entityList.add(entity); }else if(qName.equals("servlet-mapping")){ mappingList.add(mapping); } } beginTag=null; } @Override public void endDocument() throws SAXException { //文档解析结束 } public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { //获取解析工厂 SAXParserFactory factory =SAXParserFactory.newInstance(); //获取解析器 SAXParser sax =factory.newSAXParser(); //指定xml+处理器 WebHandler web = new WebHandler(); sax.parse(Thread.currentThread().getContextClassLoader() .getResourceAsStream("com/bjsxt/server/demo4/web.xml") ,web); System.out.println(web.getEntityList()); } public List<Entity> getEntityList() { return entityList; } public void setEntityList(List<Entity> entityList) { this.entityList = entityList; } public List<Mapping> getMappingList() { return mappingList; } public void setMappingList(List<Mapping> mappingList) { this.mappingList = mappingList; } } /*<?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>login</servlet-name> <servlet-class>com.bjsxt.server.demo4.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> <url-pattern>/g</url-pattern> </servlet-mapping> </web-app> 从web-app这个根节点开始,一行一行的向后读。遇到开始元素(<servlet>、<servlet-name>、<servlet-class>、<servlet-mapping>、<url-pattern>) 则会进入startElement方法,遇到结束标签(</servlet>、</servlet-name>、</servlet-class>、</servlet-mapping>、</url-pattern>) 则会进入endElement方法,其余的字符(login、/login、/g)以及空白字符则会进入characters方法。*/ /* 存储这个里面的login和com.bjsxt.server.demo4.LoginServlet <servlet> <servlet-name>login</servlet-name> <servlet-class>com.bjsxt.server.demo4.LoginServlet</servlet-class> </servlet> */ public class Entity { private String name; private String clz; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getClz() { return clz; } public void setClz(String clz) { this.clz = clz; } } /*存储这个里面的login和/login、/log <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/log</url-pattern> </servlet-mapping> */ public class Mapping { private String name; private List<String> urlPattern; public Mapping(){ urlPattern =new ArrayList<String>(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<String> getUrlPattern() { return urlPattern; } public void setUrlPattern(List<String> urlPattern) { this.urlPattern = urlPattern; } }