pull 方式解析xml文件

  1. XmlPullParser解析xml的android文档docs/reference/org/xmlpull/v1/XmlPullParser.html

  2. xmlPullParer官网:http://www.xmlpull.org/

    例子:要解析的文件:pull.xml

  3. <?xmlversion="1.0"encoding="UTF-8"?>
    <Customer>
    <PersonName>
    <SurName>张三</SurName>
    </PersonName>
    <PersonName>
    <SurName>李四</SurName>
    </PersonName>
    
    <ContactNamecontactType="CSM">
    <PersonName>
    <SurName>王五</SurName>
    </PersonName>
    <TelephonePhoneTechType="Mobile"PhoneNumber="1310000000"/>
    <Email/>
    </ContactName>
    </Customer>

    解析此文件的java文件:

  4. packagecom.xmlpullparser;
    importjava.io.InputStream;
    importjava.util.ArrayList;
    importjava.util.List;
    importandroid.os.Bundle;
    importandroid.app.Activity;
    importandroid.util.Log;
    importandroid.util.Xml;
    importandroid.widget.TextView;
    importorg.xmlpull.v1.XmlPullParser;
    publicclassMainActivityextendsActivity{
    privatestaticfinalStringTAG="xmlpullparserTest";
    TextViewmTextResult=null;
    @Override
    protectedvoidonCreate(BundlesavedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextResult=(TextView)findViewById(R.id.textView1);
    List<Customer>customers=readXMLPull();
    StringBufferstrBuffer=newStringBuffer();
    for(Customercus:customers)
    {
    strBuffer.append(cus.mSurName+","+cus.mContactType+","+cus.mPhoneTechType+","
    +cus.mPhoneNumber+"\n");
    
    }
    mTextResult.setText(strBuffer);
    }
    publicList<Customer>readXMLPull()
    {
    List<Customer>customers=null;
    Customercustomer=null;
    InputStreamis=null;
    StringBufferbuffer=null;
    intpersonCount=0;
    try{
    is=getResources().openRawResource(R.raw.pull);
    XmlPullParserxmlParser=Xml.newPullParser();
    xmlParser.setInput(is,"utf-8");
    intevtType=xmlParser.getEventType();
    while(evtType!=XmlPullParser.END_DOCUMENT)
    {
    switch(evtType)
    {
    caseXmlPullParser.START_DOCUMENT:
    buffer=newStringBuffer();
    break;
    caseXmlPullParser.END_DOCUMENT:
    break;
    caseXmlPullParser.START_TAG:
    buffer.delete(0,buffer.length());
    Stringtag=xmlParser.getName();
    Log.d(TAG,"tag="+tag);
    if(tag.equalsIgnoreCase("Customer"))
    {
    customers=newArrayList<Customer>();
    }
    elseif(tag.equalsIgnoreCase("PersonName")&&personCount<2)
    {
    customer=newCustomer();
    personCount++;
    }
    elseif(tag.equalsIgnoreCase("ContactName"))
    {
    customer=newCustomer();
    customer.mContactType=xmlParser.getAttributeValue(null,"contactType");
    }
    elseif(tag.equalsIgnoreCase("Telephone")&&customer!=null)
    {
    customer.mPhoneTechType=xmlParser.getAttributeValue(null,"PhoneTechType");
    customer.mPhoneNumber=xmlParser.getAttributeValue(null,"PhoneNumber");
    }
    break;
    caseXmlPullParser.END_TAG:
    if(xmlParser.getName().equalsIgnoreCase("PersonName")&&customer!=null)
    {
    customers.add(customer);
    if(personCount<2)
    {
    customer=null;
    }
    }
    elseif(xmlParser.getName().equalsIgnoreCase("SurName"))
    {
    customer.mSurName=buffer.toString().trim();
    }
    break;
    caseXmlPullParser.TEXT:
    Log.d(TAG,"text="+xmlParser.getText());
    buffer.append(xmlParser.getText());
    break;
    default:
    break;
    
    }
    evtType=xmlParser.next();
    }
    
    }catch(Exceptione){
    //TODO:handleexception
    e.printStackTrace();
    }
    returncustomers;
    }
    
    }

    customer.java

  5. packagecom.xmlpullparser;
    publicclassCustomer
    {
    publicStringmSurName;
    publicStringmContactType;
    publicStringmPhoneTechType;
    publicStringmPhoneNumber;
    }
  6. 效果图:

先记下来。欢迎提出意见。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


xml怎么加入图片路径
rss源错误怎么解决
文件后缀xml是什么意思
xml格式电子发票怎么获取
xml格式是什么意思
rss是什么意思啊
xml格式电子发票怎么打开
rss订阅源是什么意思
rss源是什么
xml注释怎么写
php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类