Parsing XML files in PULL mode

Source: Internet
Author: User
  1. Xmlpullparser parses XML Android documentation docs/reference/org/xmlpull/V1/xmlpullparser.html

  2. Xmlpullparer Official Website: http://www.xmlpull.org/

    Example: file to be parsed: Pull. xml

     

     

     

     

  3. <? 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:

     

  4. 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
  5. package com.xmlpullparser;public class Customer{ public String mSurName; public String mContactType; public String mPhoneTechType; public String mPhoneNumber;}
  6. :
Write down. Comments are welcome.

Parsing XML files in PULL mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.