1.SAX解析允许在读取文档的时候,即对文档进行处理,而不必等到整个文档装载完才会对文档进行操作!!!
sax采用事件处理方式解析XML文件,涉及解析器和事件处理器
解析器可以使用JAXP的API创建,只要解析到XML文档的一个组成部分,都会去调用事件处理器的一个方法,解析器在调用事件处理器的方法时,会把当前解析到的xml文档内容作为方法的参数传递给事件处理器!
事件处理器由程序员编写。
package it.git.sax;
import java.io.IOException;
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.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class Demo {
/* sax解析XML */
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
// 创建工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 创建解析器
SAXParser sp = factory.newSAXParser();
// 利用解析器得到reader
XMLReader xr = sp.getXMLReader();
// 设置事件处理器
xr.setContentHandler(new MyContentHandler());
// 利用reader 读取xml文档
xr.parse("src/book.xml");
}
}
//得到xml文档内容的事件处理器
class MyContentHandler implements ContentHandler {
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 startElement(String uri, String localName, String name,
Attributes atts) throws SAXException {
System.out.println("当前解析到了" + name + ",这是一个开始标签");
for(int i=0; i<atts.getLength();i++){
//获取标签属性
String attName=atts.getQName(i);
String attValue=atts.getValue(i);
System.out.println(attName+"="+attValue);
}
}
public void endDocument() 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 setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
}
//获取第一个节点的值
class MyContentHandler2 extends DefaultHandler {
private boolean isOk= false;
private int index=1;
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if(isOk=true&&index==1){
System.out.println(new String(ch,start,length));
}
}
@Override
public void endElement(String uri, String name, String attributes)
throws SAXException {
if(name.equals("售价")){
isOk=false;
}
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if(name.equals("售价")){
isOk=true;
index++;
}
}
}