SAX解析XML
SAX解析原理:
通过读取器读取xml文档,当读到了文档某一部分时(文档的开始,元素的开始、元素的结束、文档的结束),都会调用事件处理器的队友方法,读到的数据,以参数的形式传递给对应的方法。
SAX解析器:
SAXParser saxParserFactory= SAXParserFactory.newInstance().newSAXParser();
xml
<?xml version="1.0" encoding="UTF-8"?> <exam> <student examid="222"> <name>张三</name> <location>沈阳</location> <grade>89</grade> </student> <student idcard="333"> <name>李四</name> <location>大连</location> <grade>110</grade> </student> </exam>
代码示例:
package com.bcrj.domain; public class Student { private String name; private String location; private String grade; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } @Override public String toString() { return "Student [name=" + name + ", location=" + location + ", grade=" + grade + "]"; } }
package com.bcrj.sax; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import com.bcrj.domain.Student; public class SAXTest { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { SAXParser saxParserFactory = SAXParserFactory.newInstance() .newSAXParser(); saxParserFactory.parse("src/students.xml", new myContentHandler3()); } } // 打印文本内容 class myContentHandler1 extends DefaultHandler { @Override public void startDocument() throws SAXException { System.out.println("开始解析文档开始"); } @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { System.out.println("开始解析元素:" + qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { System.out.println("文本内容---" + new String(ch, start, length)); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub } } // 打印某一个元素文本,第二个location class myContentHandler2 extends DefaultHandler { int index = 0; boolean isLocation = false; @Override public void startDocument() throws SAXException { System.out.println("开始解析文档开始"); } @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { // System.out.println("开始解析元素:" + qName); if (qName.equals("location")) { index++; isLocation = true; } } @Override public void characters(char[] ch, int start, int length) throws SAXException { if (isLocation && index == 2) { System.out.println("文本内容---" + new String(ch, start, length)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { isLocation = false; } @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub } } // 读取XML中内容,封装到JavaBean中 class myContentHandler3 extends DefaultHandler { Student stu = null; String currentTagName = null; List<Student> students = new ArrayList<Student>(); @Override public void startDocument() throws SAXException { System.out.println("开始解析文档开始"); } @Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { if ("student".equals(qName)) { stu = new Student(); } currentTagName = qName; // System.out.println("开始解析元素:" + qName); } @Override public void characters(char[] ch, int start, int length) throws SAXException { if("name".equals(currentTagName)){ stu.setName(new String(ch,start,length)); } if("location".equals(currentTagName)){ stu.setLocation(new String(ch,start,length)); } if("grade".equals(currentTagName)){ stu.setGrade(new String(ch,start,length)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (qName.equals("student")) { students.add(stu); stu = null; } currentTagName = null; } @Override public void endDocument() throws SAXException { System.out.println("解析到了文档的结束-----打印Student"); for(Student st:students){ System.out.println(st); } } }