Sax采用时间处理的方式解析xml文件,利用SAX解析xml文档,涉及两部分内容:
1.解析器
2.时间处理器
解析其可以利用JAXP的API创建,创建SAX解析器之后,就可以制定解析器去解析某个xml文档
解析其采用SAX方式在解析某个xml文档时,他只要解析到xml文档的一个组成部分,都回去调用事件处理器的一个方法。解析器在调用事件处理器的方法时,会把当前解析到的xml内容作为方法的参数传递给事件处理器。
事件处理器由程序员编写,程序员通过事件处理器中方法的参数,就可以很轻松地得到SAX解析器解析到的数据,从而可以决定如何对数据进行处理。
可以简单表示为:
Xml文档 <------- SAX解析器 --------------> 事件处理器
SAX解析器的模型:
其中包含了4种处理器,常用的是你内容处理器(Content Handler)。
在官方文档中,ContentHandler的事件处理器的方法如下,每个方法都由相应的事件激发,并且获得相应的一部分xml文档的内容。
Modifier and Type | Method and Description |
---|---|
void | characters(char[] ch, int start,int length) Receive notification of character data. |
void | endDocument() Receive notification of the end of a document. |
void | endElement(String uri,String localName,String qName) Receive notification of the end of an element. |
void | endPrefixMapping(String prefix) End the scope of a prefix-URI mapping. |
void | ignorableWhitespace(char[] ch, int start, int length) Receive notification of ignorable whitespace in element content. |
void | processingInstruction(String target, String data) Receive notification of a processing instruction. |
void | setDocumentLocator(Locator locator) Receive an object for locating the origin of SAX document events. |
void | skippedEntity(String name) Receive notification of a skipped entity. |
void | startDocument() Receive notification of the beginning of a document. |
void | startElement(String uri,String localName, String qName, Attributes atts) Receive notification of the beginning of an element. |
void | startPrefixMapping(String prefix,String uri) Begin the scope of a prefix-URI Namespace mapping. |
下面个例子入个门吧。
xml文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE SwordLibrary SYSTEM "SwordTypeDefinition.dtd">
<SwordLibrary>
<Sword sno="s1">
<SwordName>欢欣之刃</SwordName>
<Price>1000</Price>
<Attack factor="1.0">10</Attack>
</Sword>
<Sword sno="s2">
<SwordName>夜叉</SwordName>
<Price>2050</Price>
<Attack factor="2.0">30</Attack>
</Sword>
<Sword sno="s3">
<SwordName>魔棒</SwordName>
<Price type="Dollar">200</Price>
<Attack factor="1.0">0</Attack>
</Sword>
</SwordLibrary>
java代码:
package JavaLeaner.XmlTest;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
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 XmlDemo4 {
@Test
public void TestMain() throws ParserConfigurationException, SAXException, IOException
{
// 1.创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 2.得到SAX解析器
SAXParser sax = factory.newSAXParser();
// 3.得到读取器
XMLReader reader = sax.getXMLReader();
// 4.设置内容处理器(即事件处理器:SAX解析器获取一部分内容即触发某一个事件,相应的事件解析器完成相应处理)
reader.setContentHandler(new TestListAllHandler());
// 5.读取xml文档内容
reader.parse("src/JavaLeaner/XmlTest/SwordLib.xml");
}
}
class TestListAllHandler implements ContentHandler {
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
System.out.println("characters");
System.out.println(new String(ch,start,length));
System.out.println("");
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
System.out.println("endDocument");
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
System.out.println("endElement");
System.out.println("uri:"+uri);
System.out.println("localName:"+localName);
System.out.println("qName:"+qName);
System.out.println("");
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
@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 setDocumentLocator(Locator locator) {
// 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
System.out.println("startDocument");
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
// TODO Auto-generated method stub
System.out.println("startElement");
System.out.println("uri:"+uri);
System.out.println("localName:"+localName);
System.out.println("qName:"+qName);
System.out.println(qName+"的属性如下:");
for(int i=0;atts!=null&&i<atts.getLength();i++)
{
//attributes类需要注意不要导错包
//attributes类getValue有多个重载,支持按index或qName来查询
String attrName = atts.getQName(i);
String attrValue = atts.getValue(attrName);
System.out.println(" attrName:"+attrValue);
}
System.out.println("");
}
@Override
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
}
执行结果如下:
startDocument
startElement
uri:
localName:
qName:SwordLibrary
SwordLibrary的属性如下:
startElement
uri:
localName:
qName:Sword
Sword的属性如下:
attrName:s1
startElement
uri:
localName:
qName:SwordName
SwordName的属性如下:
characters
欢欣之刃
endElement
uri:
localName:
qName:SwordName
startElement
uri:
localName:
qName:Price
Price的属性如下:
characters
1000
endElement
uri:
localName:
qName:Price
startElement
uri:
localName:
qName:Attack
Attack的属性如下:
attrName:1.0
characters
10
endElement
uri:
localName:
qName:Attack
endElement
uri:
localName:
qName:Sword
startElement
uri:
localName:
qName:Sword
Sword的属性如下:
attrName:s2
startElement
uri:
localName:
qName:SwordName
SwordName的属性如下:
characters
夜叉
endElement
uri:
localName:
qName:SwordName
startElement
uri:
localName:
qName:Price
Price的属性如下:
characters
2050
endElement
uri:
localName:
qName:Price
startElement
uri:
localName:
qName:Attack
Attack的属性如下:
attrName:2.0
characters
30
endElement
uri:
localName:
qName:Attack
endElement
uri:
localName:
qName:Sword
startElement
uri:
localName:
qName:Sword
Sword的属性如下:
attrName:s3
startElement
uri:
localName:
qName:SwordName
SwordName的属性如下:
characters
魔棒
endElement
uri:
localName:
qName:SwordName
startElement
uri:
localName:
qName:Price
Price的属性如下:
attrName:Dollar
characters
200
endElement
uri:
localName:
qName:Price
startElement
uri:
localName:
qName:Attack
Attack的属性如下:
attrName:1.0
characters
0
endElement
uri:
localName:
qName:Attack
endElement
uri:
localName:
qName:Sword
endElement
uri:
localName:
qName:SwordLibrary
endDocument