Android之SAX解析笔记

books.xml:

<?xml version="1.0" encoding="utf-8"?>
<books>
    <book id="12" code="a">
        <name>thinking in java</name>
        <price>85.5</price>
    </book>
    <book id="15" code="b">
        <name>Spring in Action</name>
        <price>39.0</price>
    </book>
</books>
View Code

 

MyContentHandler.java:

package com.example.xmlparse;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyContentHandler extends DefaultHandler {
    private String name;
    private String price;
    private String tagName;
    
    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        System.out.println(".............startDocument.............");
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        System.out.println(".............endDocument.............");
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        System.out.println(".............startElement.............");
        tagName = localName;
        if(localName.equals("book")) {
            // 获取标签的全部属性
            for(int i=0; i<attributes.getLength(); i++) {
                System.out.println(attributes.getLocalName(i) + " = " + attributes.getValue(i));
            }
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        System.out.println(".............endElement.............");
        tagName = "";
        if(localName.equals("book")) {
            this.printout();
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub
        System.out.println(".............characters.............");
        System.out.println(tagName);
        if(tagName.equals("name")) {
            name = new String(ch, start, length);
        }else if(tagName.equals("price")) {
            price = new String(ch, start, length);
        }
    }
    
    private void printout() {
        System.out.println("name:" + name);
        System.out.println("price:" + price);
    }
}
View Code

 

执行代码结果:

.............startDocument.............
.............startElement.............
.............characters.............
books
.............characters.............
books
.............startElement.............
id = 12
code = a
.............characters.............
book
.............characters.............
book
.............startElement.............
.............characters.............
name
.............endElement.............
.............characters.............
.............characters.............
.............startElement.............
.............characters.............
price
.............endElement.............
.............characters.............
.............characters.............
.............endElement.............
name:thinking in java
price:85.5
.............characters.............
.............characters.............
.............startElement.............
id = 15
code = b
.............characters.............
book
.............characters.............
book
.............startElement.............
.............characters.............
name
.............endElement.............
.............characters.............
.............characters.............
.............startElement.............
.............characters.............
price
.............endElement.............
.............characters.............
.............characters.............
.............endElement.............
name:Spring in Action
price:39.0
.............characters.............
.............endElement.............
.............endDocument.............
View Code

 

 

注意:

XML报文中行尾的\n跟行首的\t都会调用characters

 

posted @ 2015-09-06 14:40  大盗—如风  阅读(284)  评论(0编辑  收藏  举报