XML数据解析

2017-08-23  本文已影响0人  TakeItEasyJQ

这只是我在学Android过程中对于所学知识的巩固和方便日后查询的学习笔记,能帮助到有需要的和我一样的初学者就更好了

XML的解析有Pull和SAX两种方式(SAX要继承DefaultHandler类)

<apps>
    <app>
        <id>1</id>
        <name>Googlemap</name>
        <version>1.0</veision>
    </app>
<app>
        <id>2</id>
        <name>Chrome</name>
        <version>2.1</veision>
    </app>
    <app>
        <id>3</id>
        <name>GooglePlay</name>
        <version>2.3</veision>
    </app>
</apps>

Pull

privare void parseXMLWithPull(String xmlData){
    try{
        XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
        XmlPullParser xmlPullParser=factory.newPullParser();
        xmlPullParser.setInput(new StringReader(xmlData));
        int eventType=xmlPullParser.getEventType();
        String id=" " ;String name=" " ;String version=" ";
        While(eventType!=xmlPullParser.END_DOCUMENT){ //结尾节点标记
            String nodeName=xmlPullParser.getName();    //节点名称
            switch(eventType){
                case XmlPullParser.START_TAG:     //<apps>、<app>、<id>等
                    if("id".equals(nodeName)){
                        id=xmlPullParser.nextText();
                    }else if("name".equals(nodeName)){
                        name=xmlPullParser.nextText();
                    }else if("veision".equals(nodeName)){
                        veison=xmlPullParser.nextText();
                    }
                    break;
                case XmlPullParser.END_TAG :       //</apps>、</apps>等
                    if("app".equals(nodeName)){
                        //相应的逻辑
                    }
//由此可知Pull为一行一行读取后寻找与给定值相同的节点名称,找到后提取值
//START_TAG与END_TAG间包含的为上例中一个<app></app>,例中 共3个
                default:
                    break;
          }
        }
    }
}

SAX

public class SAXHandler extends DefaultHandler{
    private String nodeName;  private StringBuilder id;
    Private StringBuilder name; private StringBuilder version;
    @override
    public void startDocument() throws SAXException{  //开始解析时调用
        id=new StringBuilder();
        name=new StringBuilder();
        version=new StringBuilder();
    }
    @override   
    public void startElement(String uri ,String LocalName ,String qName 
                                                      ,Attributes attributes) throws SAXException{
    //开始解析某一节点时调用
        nodeName=LocalName;  //获取节点名
    }
    @override
    public void characters(char[] ch ,int start ,int length) throws SAXException{
    //根据当前节点名自动判断添加至哪一StringBuilder中
        if("id".equals(nodeName)){
            id.append(ch ,start ,length);
        }else  if("name".equals(nodeName)){
            id.append(ch ,start ,length);
        }else  if("version".equals(nodeName)){
            id.append(ch ,start ,length);
        }
    }
    @override  
    public void endElement(String uri ,String localName ,String qName) throws
                                                                                      SAXException{
    //某一节点解析完成后调用
        if("app".equals(nodeName)){
            //自己的逻辑。可能有回车或转行符,调用trim()去掉id.toString().trim();
            id.setLength(0) ;name.setLength(0) ;version.setLength(0); //清空
        }
    }
    @override
    public void endDocument() throws SAXException{
        super.endDocument();
    }
}

使用时

private void parseXMLWithSAX(String xmlData){
    try{
         SAXParserFactory factory=SAXParserFactory.newInstance();
         XMLReader xmlReader=factory.newSAXParser().getXMLReader();
        SAXHandler handler=new SAXHandler();
        xmlReader.setContentHandler(handler);
        xmlParser.parse(new inputSource(new StringReader(xmlData)));
    }catch(SAXException e){
        e.printStackTrace();
    }
}
上一篇下一篇

猜你喜欢

热点阅读