import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
/**
* 1.新建一个SAX解析工厂实例
* 2.new一个解析器
* 3.new一个解析处理器
* 4.解析文件
* 5.获取信息
* @author Administrator
*
*/
public class SAXParserDemo {
public static void main(String[] args) {
SAXParserFactory factory = SAXParserFactory.newInstance();// 工厂模式,单例模式
try {
/* 获得一个sax解析器 */
SAXParser parser = factory.newSAXParser();
/* 解析器解析这个文件,new一个解析处理器 */
SAXParserHandler sax = new SAXParserHandler();
parser.parse("books.xml", sax);
System.out.println("几本书:" + sax.booklist.size());
for (Book book : sax.booklist) {
System.out.println(book.getId());
System.out.println(book.getName());
System.out.println(book.getPrice());
System.out.println(book.getAuthor());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
1 package SAXParser1;
2
3 import java.util.ArrayList;
4
5 import org.xml.sax.Attributes;
6 import org.xml.sax.SAXException;
7 import org.xml.sax.helpers.DefaultHandler;
8
9 /**
10 * @author xuhua 解析处理器
11 * 1.重写方法
12 */
13 public class SAXParserHandler extends DefaultHandler {
14 /* 全局变量 */
15 Book book;
16 String value;
17 ArrayList<Book> booklist = new ArrayList<Book>();
18
19 /**
20 * 解析开始
21 */
22 @Override
23 public void startDocument() throws SAXException {
24 super.startDocument();
25 System.out.println("parse start");
26 }
27
28 /**
29 * 解析结束
30 */
31 @Override
32 public void endDocument() throws SAXException {
33 super.endDocument();
34 System.out.println("parse end");
35 }
36
37 /**
38 * 开始解析标签
39 * qName:标签名字 Attributes:属性
40 */
41 @Override
42 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
43
44 super.startElement(uri, localName, qName, attributes);
45
46 if (qName.equals("book")) {
47 book = new Book();
48 String id = attributes.getValue("id");//获取id属性值
49 System.out.println(id);
50
51 /* 获取属性名与属性值 */
52 int num = attributes.getLength(); // 属性个数
53 for (int i = 0; i < num; i++) {
54 System.out.println("属性名:" + attributes.getQName(i));
55 System.out.println("属性值:" + attributes.getValue(i));
56 /* 把id属性值set进book实体中 */
57 if (attributes.getQName(i).equals("id")) {
58 book.setId(attributes.getValue(i));
59 }
60 }
61 }
62
63 System.out.println("属性名:" + qName);
64 }
65
66 /**
67 * 结束标签解析
68 */
69 @Override
70 public void endElement(String uri, String localName, String qName) throws SAXException {
71
72 super.endElement(uri, localName, qName);
73 if (qName.equals("book")) {
74 booklist.add(book);
75 book = null;
76 System.out.println("==============结束遍历某一本书的内容=============");
77 }
78 /* id为book节点的属性,其他为节点值 */
79 // else if(qName.equals("id"))
80 // {
81 // book.setId(value);
82 // }
83 else if (qName.equals("name")) {
84 book.setName(value);
85 } else if (qName.equals("price")) {
86 book.setPrice(value);
87 } else if (qName.equals("author")) {
88 book.setAuthor(value);
89 }
90 System.out.println(qName + "元素结束");
91
92 }
93
94 /**
95 * 获取节点值
96 */
97 @Override
98 public void characters(char[] ch, int start, int length) throws SAXException {
99
100 super.characters(ch, start, length);
101 /**/
102 value = new String(ch, start, length);
103 // if(value.trim()!=null)
104 if (!value.trim().equals("")) // 标签之间存在空格
105 System.out.println(value);
106 }
107
108 }