标签:android http io os ar java for 文件 sp
XmlPullParser解析xml的android文档docs/reference/org/xmlpull/v1/XmlPullParser.html
xmlPullParer官网:http://www.xmlpull.org/
例子:要解析的文件:pull.xml
<?xml version="1.0" encoding="UTF-8"?> <Customer> <PersonName> <SurName>张三</SurName> </PersonName> <PersonName> <SurName>李四</SurName> </PersonName> <ContactName contactType="CSM"> <PersonName> <SurName>王五</SurName> </PersonName> <Telephone PhoneTechType="Mobile" PhoneNumber="1310000000"/> <Email /> </ContactName> </Customer>解析此文件的java文件:
package com.xmlpullparser; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.util.Xml; import android.widget.TextView; import org.xmlpull.v1.XmlPullParser; public class MainActivity extends Activity { private static final String TAG = "xmlpullparserTest"; TextView mTextResult = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextResult = (TextView)findViewById(R.id.textView1); List<Customer> customers = readXMLPull(); StringBuffer strBuffer = new StringBuffer(); for(Customer cus:customers) { strBuffer.append(cus.mSurName+", "+cus.mContactType+","+cus.mPhoneTechType+"," +cus.mPhoneNumber+"\n"); } mTextResult.setText(strBuffer); } public List<Customer> readXMLPull() { List<Customer> customers = null; Customer customer = null; InputStream is = null; StringBuffer buffer = null; int personCount = 0; try { is = getResources().openRawResource(R.raw.pull); XmlPullParser xmlParser = Xml.newPullParser(); xmlParser.setInput(is,"utf-8"); int evtType = xmlParser.getEventType(); while(evtType!=XmlPullParser.END_DOCUMENT) { switch(evtType) { case XmlPullParser.START_DOCUMENT: buffer = new StringBuffer(); break; case XmlPullParser.END_DOCUMENT: break; case XmlPullParser.START_TAG: buffer.delete(0, buffer.length()); String tag = xmlParser.getName(); Log.d(TAG, "tag="+tag); if(tag.equalsIgnoreCase("Customer")) { customers = new ArrayList<Customer>(); } else if(tag.equalsIgnoreCase("PersonName")&&personCount<2) { customer = new Customer(); personCount++; } else if(tag.equalsIgnoreCase("ContactName")) { customer = new Customer(); customer.mContactType = xmlParser.getAttributeValue(null, "contactType"); } else if(tag.equalsIgnoreCase("Telephone")&&customer!=null) { customer.mPhoneTechType = xmlParser.getAttributeValue(null, "PhoneTechType"); customer.mPhoneNumber = xmlParser.getAttributeValue(null, "PhoneNumber"); } break; case XmlPullParser.END_TAG: if(xmlParser.getName().equalsIgnoreCase("PersonName")&&customer!=null) { customers.add(customer); if(personCount<2) { customer = null; } } else if(xmlParser.getName().equalsIgnoreCase("SurName")) { customer.mSurName = buffer.toString().trim(); } break; case XmlPullParser.TEXT: Log.d(TAG, "text ="+xmlParser.getText()); buffer.append(xmlParser.getText()); break; default: break; } evtType = xmlParser.next(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return customers; } }customer.java
package com.xmlpullparser; public class Customer { public String mSurName; public String mContactType; public String mPhoneTechType; public String mPhoneNumber; }
标签:android http io os ar java for 文件 sp
原文地址:http://my.oschina.net/u/172649/blog/324417