xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE 书架 [
<!ELEMENT 书架 (书+)>
<!ELEMENT 书 (书名,作者,售价)>
<!ELEMENT 书名 (#PCDATA)>
<!ELEMENT 作者 (#PCDATA)>
<!ELEMENT 售价 (#PCDATA)>
]>
<书架>
<书>
<书名 name="xxx">钢铁是怎样炼成的</书名>
<作者>尼·奥斯特洛夫斯基 </作者>
<售价>16.5</售价>
</书>
<书>
<书名>假如给我三天光明</书名>
<作者>海伦·凯勒 </作者>
<售价>17.6</售价>
</书>
</书架>
一、得到xml文档中的所有类容;
package cn.sax;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
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;
//sax解析xml文档
public class Demo1 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//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{
public void startElement(String uri, String localName, String name,
Attributes atts) throws SAXException {
System.out.println("<"+name+">");
for (int i=0;atts!=null && i<atts.getLength();i++){
String index = atts.getQName(i); //得到属性名
String value = atts.getValue(i); //得到属性值
System.out.println(index + "=" + value);
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
System.out.println(new String(ch,start,length));
}
public void endElement(String uri, String localName, String name)
throws SAXException {
System.out.println("</"+name+">");
}
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
}
public void processingInstruction(String target, String data)
throws SAXException {
// TODO Auto-generated method stub
}
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
}
二、获取指定标签的值
package cn.sax;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.validation.Schema;
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;
import org.xml.sax.helpers.DefaultHandler;
//sax解析xml文档
public class Demo2 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//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(currenttag.equalsIgnoreCase("作者")){
currentNumber++;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if("作者".equals(currenttag) && currentNumber==needNumber){
System.out.println(new String(ch,start,length));
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currenttag=null;
}
}
三、Sax解析案例javabean封装xml文档数据对象
package cn.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 javax.xml.validation.Schema;
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;
import org.xml.sax.helpers.DefaultHandler;
//sax解析xml文档
public class Demo3 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//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.getList();
Book book=list.get(1);
System.out.println(book.getName());
System.out.println(book.getAuthor());
System.out.println(book.getPrice());
}
}
//获取指定标签的值
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 characters(char[] ch, int start, int length)
throws SAXException {
if("书名".equals(currentTag)){
String name = new String(ch,start,length);
book.setName(name);
}
if("作者".equals(currentTag)){
String author = new String(ch,start,length);
book.setAuthor(author);
}
if("售价".equals(currentTag)){
String price = new String(ch,start,length);
book.setPrice(price);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if(qName.equals("书")){
list.add(book);
book = null;
}
currentTag=null;
}
public List getList() {
return list;
}
}