Xmlpullparser parses XML Android documentation docs/reference/org/xmlpull/V1/xmlpullparser.html
Xmlpullparer Official Website: http://www.xmlpull.org/
Example: file to be parsed: Pull. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Customer> <personname> <surname> Zhang San </surname> </personname> <surname> Li Si </surname> </personname> <contactname contacttype = "csm"> <personname> <surname> Wang Wu </surname> </personname> <telephone phonetechtype = "mobile" phonenumber = "1310000000"/> <email/> </contactname> </ customer>
The Java file for parsing this file:
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;}
- :
Write down. Comments are welcome.
Parsing XML files in PULL mode