XmlPullParser解析xml的android文档docs/reference/org/xmlpull/v1/XmlPullParser.html
xmlPullParer官网:http://www.xmlpull.org/
例子:要解析的文件:pull.xml
<?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>
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
packagecom.xmlpullparser; publicclassCustomer { publicStringmSurName; publicStringmContactType; publicStringmPhoneTechType; publicStringmPhoneNumber; }
效果图:
先记下来。欢迎提出意见。
原文链接:https://www.f2er.com/xml/297928.html