Java学习之Xml系列四:JAXP的另一种解析XML的方法——SAX解析


Sax采用时间处理的方式解析xml文件,利用SAX解析xml文档,涉及两部分内容:

1.解析器

2.时间处理器

解析其可以利用JAXP的API创建,创建SAX解析器之后,就可以制定解析器去解析某个xml文档

解析其采用SAX方式在解析某个xml文档时,他只要解析到xml文档的一个组成部分,都回去调用事件处理器的一个方法。解析器在调用事件处理器的方法时,会把当前解析到的xml内容作为方法的参数传递给事件处理器。

事件处理器由程序员编写,程序员通过事件处理器中方法的参数,就可以很轻松地得到SAX解析器解析到的数据,从而可以决定如何对数据进行处理。

可以简单表示为:

Xml文档 <-------  SAX解析器 -------------->   事件处理器


SAX解析器的模型:

其中包含了4种处理器,常用的是你内容处理器(Content Handler)。

1346409301_1540.jpg

在官方文档中,ContentHandler的事件处理器的方法如下,每个方法都由相应的事件激发,并且获得相应的一部分xml文档的内容。

Modifier and TypeMethod and Description
voidcharacters(char[] ch, int start,int length)

Receive notification of character data.

voidendDocument()

Receive notification of the end of a document.

voidendElement(String uri,String localName,String qName)

Receive notification of the end of an element.

voidendPrefixMapping(String prefix)

End the scope of a prefix-URI mapping.

voidignorableWhitespace(char[] ch, int start, int length)

Receive notification of ignorable whitespace in element content.

voidprocessingInstruction(String target,  String data)

Receive notification of a processing instruction.

voidsetDocumentLocator(Locator locator)

Receive an object for locating the origin of SAX document events.

voidskippedEntity(String name)

Receive notification of a skipped entity.

voidstartDocument()

Receive notification of the beginning of a document.

voidstartElement(String uri,String localName, String qName, Attributes atts)

Receive notification of the beginning of an element.

voidstartPrefixMapping(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















转载于:https://my.oschina.net/happyBKs/blog/297949

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值