pull解析 讲解

关于PULL解析XML

1.网上关于解释pull解析xml的例子很多了,大同小异!

 

解析一个有点复杂的xml

先看xml代码

<?xml version="1.0" encoding="UTF-8"?>
<app-config>
    <global>
        <version>
            1.0
        </version>
        <update-date>
            2013-04-03 15:05:05
        </update-date>
        <md5>
            11111111    
        </md5>
    </global>
    <media-class-list>
        <media-class>
            <id>1</id>
            <name>热菜</name>            
            <pid>0</pid>
            <order>0</order>
            <file-info-id-list>
                <file-info-id>
                    <id>1</id>
                    <order>0</order>
                </file-info-id>
                <file-info-id>
                    <id>5</id>
                    <order>1</order>
                </file-info-id>    
                <file-info-id>
                    <id>8</id>
                    <order>2</order>
                </file-info-id>            
            </file-info-id-list>
        </media-class>
        <media-class>
            <id>4</id>
            <name>凉菜</name>            
            <pid>0</pid>
            <order>1</order>            
            <file-info-id-list>
                <file-info-id>
                    <id>4</id>
                    <order>0</order>
                </file-info-id>

            </file-info-id-list>            
        </media-class>        
        <media-class>
            <id>6</id>
            <name>酒水</name>            
            <pid>0</pid>
            <order>2</order>
            <file-info-id-list>
                <file-info-id>
                    <id>2</id>
                    <order>0</order>
                </file-info-id>

            </file-info-id-list>

        </media-class>
        <media-class>
            <id>5</id>
            <name>主食</name>            
            <pid>0</pid>
            <order>3</order>
            <file-info-id-list>
                <file-info-id>
                    <id>3</id>
                    <order>0</order>
                </file-info-id>
            </file-info-id-list>

        </media-class>
        
        <media-class>
            <id>2</id>
            <name>火锅</name>            
            <pid>1</pid>
            <file-info-id-list>
                <file-info-id>
                    <id>6</id>
                    <order>0</order>
                </file-info-id>
                <file-info-id>
                    <id>7</id>
                    <order>0</order>
                </file-info-id>

            </file-info-id-list>
            <order>0</order>            
        </media-class>
        <media-class>
            <id>3</id>
            <name>小炒</name>            
            <pid>1</pid>
            <order>1</order>            
        </media-class>
        
        <media-class>
            <id>7</id>
            <name>红酒</name>            
            <pid>6</pid>
            <order>0</order>
        </media-class>
        <media-class>
            <id>8</id>
            <name>白酒</name>            
            <pid>6</pid>
            <order>1</order>
        </media-class>        
    </media-class-list>
    
    <file-info-list>
        <file-info>
            <id>
                1
            </id>
            <name>
            红烧鱼块
            </name>
            <file-path>
                aa/hsyk.jpg
            </file-path>
            <pix-x>
                1024
            </pix-x>
            <pix-y>768</pix-y>
            <memo>可能会很多</memo>            
        </file-info>
        <file-info>
            <id>
                2
            </id>
            <name>
            二锅头
            </name>
            <file-path>
                bb/egt.jpg
            </file-path>
            <pix-x>
                1024
            </pix-x>
            <pix-y>768</pix-y>
            <memo>可能会很多</memo>
        </file-info>
        <file-info>
            <id>
                3
            </id>
            <name>
            馒头
            </name>
            <file-path>
                bb/mt.jpg
            </file-path>
            <pix-x>
                1024
            </pix-x>
            <pix-y>768</pix-y>
            <memo>可能会很多</memo>
        </file-info>
        <file-info>
            <id>
                4
            </id>
            <name>
            东北凉粉
            </name>
            <file-path>
                bb/dblf.jpg
            </file-path>
            <pix-x>
                1024
            </pix-x>
            <pix-y>768</pix-y>
            <memo>可能会很多</memo>
        </file-info>                
        <file-info>
            <id>
                5
            </id>
            <name>
            小炒肉
            </name>
            <file-path>
                bb/xcr.jpg
            </file-path>
            <pix-x>
                1024
            </pix-x>
            <pix-y>768</pix-y>
            <memo>可能会很多</memo>
        </file-info>                        
        <file-info>
            <id>
                6
            </id>
            <name>
            四川火锅
            </name>
            <file-path>
                bb/schg.jpg
            </file-path>
            <pix-x>
                1024
            </pix-x>
            <pix-y>768</pix-y>
            <memo>可能会很多</memo>
        </file-info>                        
        <file-info>
            <id>
                7
            </id>
            <name>
            东北火锅
            </name>
            <file-path>
                bb/dbgg.jpg
            </file-path>
            <pix-x>
                1024
            </pix-x>
            <pix-y>768</pix-y>
            <memo>可能会很多</memo>
        </file-info>    
                <file-info>
            <id>
                8
            </id>
            <name>
            湖南火锅
            </name>
            <file-path>
                bb/hngg.jpg
            </file-path>
            <pix-x>
                1024
            </pix-x>
            <pix-y>768</pix-y>
            <memo>可能会很多</memo>
        </file-info>                                    
    </file-info-list>
</playlist-config>

这个xml的元素间都有关联,而且有名字重复的节点。

接下来就是我解析xml的代码

public List<AppFileInfo> readFileinfoList(XmlPullParser pullParser) throws Exception, IOException {
        boolean bStop = false;
        List<AppFileInfo> listAppFileInfo= null;
        int eventType = pullParser.getEventType();
        while(eventType!=XmlPullParser.END_DOCUMENT){
            String nodeName = pullParser.getName();
            switch (eventType) {
            case XmlPullParser.START_TAG:                
                if("file-info-list".equals(nodeName)){
                    listAppFileInfo= new ArrayList<AppFileInfo>();
                } else if ("file-info".equals(nodeName) ) {
                    AppFileInfo appFileInfo = null;                    
                    appFileInfo = readFileinfo( pullParser );
                    listAppFileInfo.add(appFileInfo);            
                }
                break;
                
            case XmlPullParser.END_TAG:
                if("file-info-list".equals(nodeName)){
                    bStop = true;
                    break;
                }
                
            }
            if ( bStop == true ) {
                break;
            }
            
            eventType = pullParser.next();
        }
        return listAppFileInfo;
    }
    public AppFileInfo readFileinfo(XmlPullParser pullParser) throws XmlPullParserException, IOException {
        boolean bStop = false;    
        AppFileInfo fileInfo = null;
        int eventType = pullParser.getEventType();
        while(eventType!=XmlPullParser.END_DOCUMENT){
            String nodeName = pullParser.getName();
            switch (eventType) {
            case XmlPullParser.START_TAG:                
                if("file-info".equals(nodeName)){
                    fileInfo=new AppFileInfo();
                }else if("id".equals(nodeName)){    
                    int i = Integer.parseInt(pullParser.nextText().trim().toString());
                    fileInfo.setId(i);
                }else if("name".equals(nodeName)){
                    String str =pullParser.nextText().trim().toString();
                    fileInfo.setName(str);

                }
                else if("file-path".equals(nodeName)){
                    fileInfo.setFilepart(pullParser.nextText().trim().toString());

                }
                else if("pix-x".equals(nodeName)){
                    fileInfo.setPix_x(Integer.parseInt(pullParser.nextText().trim().toString()));
                }
                else if("pix-y".equals(nodeName)){
                    fileInfo.setPix_y(Integer.parseInt(pullParser.nextText().trim().toString()));
                }
                else if("memo".equals(nodeName)){
                    fileInfo.setMemo(pullParser.nextText().trim().toString());
                }
                
                break;
                
            case XmlPullParser.END_TAG:
                if("file-info".equals(nodeName)){
                    bStop = true;                    
                }
                break;
            }
            if ( bStop == true ) {
                break;
            }
            eventType = pullParser.next();            
        }
        return fileInfo;
    }
    public List<AppFileInfo> readAppFileInfo( InputStream inputStream,String inputEncoding ){

        XmlPullParser pullParse = Xml.newPullParser();    
        try {
            ReadAppXml Pulltool = new ReadAppXml();
            pullParse.setInput(inputStream, inputEncoding);
            List<AppFileInfo> appFileInfoList = Pulltool.readFileinfoList(pullParse);
            inputStream.close();
            return appFileInfoList;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        
    }

最后说一句,用pull解析xml的可以解释手机的内存

posted @ 2013-04-25 17:32  大暴雨  阅读(389)  评论(0)    收藏  举报