加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

pull 方式解析xml文件

发布时间:2020-12-15 22:21:44 所属栏目:百科 来源:网络整理
导读:XmlPullParser解析xml的android文档docs/reference/org/xmlpull/v1/XmlPullParser.html xmlPullParer官网:http://www.xmlpull.org/ 例子:要解析的文件:pull.xml ?xmlversion="1.0"encoding="UTF-8"?CustomerPersonNameSurName张三/SurName/PersonNamePersonN
  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. 效果图:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读