1.Book.java
package cn.itcast.sax;
public class Book {
private String name;
private String author;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
2.Demo1.java
package cn.itcast.sax;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class Demo1 {
/*
* sax 解析xml文档
*/
@Test
public void test1() throws Exception, SAXException {
// 1.创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 2.得到解析器
SAXParser sp = factory.newSAXParser();
// 3.得到读取器
XMLReader reader = sp.getXMLReader();
// 4.设置内容处理器
reader.setContentHandler(new ListHandler());
// 5.读取xml文档内容
reader.parse("src/book.xml");
}
}
// 得到xml文档所有内容
class ListHandler implements ContentHandler {
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
System.out.println("<" + qName + ">");
for (int i = 0; atts != null & i < atts.getLength(); i++) {
String attName = atts.getQName(i);
String attValue = atts.getValue(i);
System.out.println(attName + "=" + attValue);
}
}
@Override
public void endElement(String uri, String localName, String qName)
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 ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void processingInstruction(String target, String data)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
}
3.Demo2.java
package cn.itcast.sax;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class Demo2 {
/*
* sax 解析xml文档
*/
@Test
public void test1() throws Exception, SAXException {
// 1.创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 2.得到解析器
SAXParser sp = factory.newSAXParser();
// 3.得到读取器
XMLReader reader = sp.getXMLReader();
// 4.设置内容处理器
reader.setContentHandler(new TagValueHandler());
// 5.读取xml文档内容
reader.parse("src/book.xml");
}
// 获取指定标签的值
class TagValueHandler extends DefaultHandler {
private String currentTag;// 记住当前解析到的是什么标签
private int needNumber=2;//记住想获取第几个作者标签的值
private int currentNumber;//当前解析到的是第几个
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentTag = qName;
if ("作者".equals(currentTag))
currentNumber++;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentTag = null;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if ("作者".equals(currentTag)&¤tNumber==needNumber)
System.out.println(new String(ch, start, length));
}
}
}
4.Demo3.java
package cn.itcast.sax;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import cn.itcast.sax.Demo2.TagValueHandler;
public class Demo3 {
/*
* sax 解析xml文档
*/
@Test
public void test1() throws Exception, SAXException {
// 1.创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 2.得到解析器
SAXParser sp = factory.newSAXParser();
// 3.得到读取器
XMLReader reader = sp.getXMLReader();
// 4.设置内容处理器
BeanListHandler handler=new BeanListHandler();
reader.setContentHandler(handler);
// 5.读取xml文档内容
reader.parse("src/book.xml");
List<Book> list=handler.getBooks();
for(Book book :list){
System.out.println("书名:"+book.getName());
System.out.println("作者:"+book.getAuthor());
System.out.println("价格:"+book.getPrice());
System.out.println();
}
}
// 把xml文档中的每一本书封装到一个book对象,并把多个book对象放在一个list集合中返回
class BeanListHandler extends DefaultHandler {
private List list = new ArrayList();
private String currentTag;
private Book book;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentTag = qName;
if ("书".equals(currentTag))
book = new Book();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if ("书".equals(qName)) {
list.add(book);
book = null;
}
currentTag = null;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if ("书名".equals(currentTag)) {
String value = new String(ch, start, length);
book.setName(value);
}
if ("作者".equals(currentTag)) {
String value = new String(ch, start, length);
book.setAuthor(value);
}
if ("售价".equals(currentTag)) {
String value = new String(ch, start, length);
book.setPrice(value);
}
}
public List getBooks() {
return list;
}
}
}