pull 方式解析xml文件

前端之家收集整理的这篇文章主要介绍了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. 效果图:

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

原文链接:https://www.f2er.com/xml/297928.html

猜你在找的XML相关文章