android-xml檔案解析教程

來源:互聯網
上載者:User

在討論群裡裡面有人在問xml的解析方法,我就弄了個簡單的demon來介紹我自己的方法下面的xml檔案和讀取的

 

Xml代碼 
<?xml version="1.0" encoding="utf-8"?> 
<tests> 
    <test name="test_1">測試1</test> 
    <test name="test_2">測試2</test> 
    <test name="test_3">測試3</test> 
    <test name="test_4">測試4</test> 
    <test name="test_5">測試5</test> 
    <test name="test_6">測試6</test> 
    <test name="test_7">測試7</test> 
    <test name="test_8">測試8</test> 
    <test name="test_9">測試9</test> 
</tests> 


 :

<br><br><img src="http://www.bkjia.com/uploadfile/2011/0921/20110921013025587.png" alt=""><br> 主要代碼

 


Java代碼 
package com.wjh.xmldemon; 
 
import java.util.ArrayList; 
 
import org.xmlpull.v1.XmlPullParser; 
 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.XmlResourceParser; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class XmlDemonActivity extends Activity { 
    /** Called when the activity is first created. */ 
    TextView tv = null; 
    ArrayList<Test> list = null; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        tv =(TextView)findViewById(R.id.tv); 
        list = getXmlData(this, R.xml.test); 
        initData(); 
    } 
    //初始化資料 
    private void initData() 
    { 
        StringBuffer sb = new StringBuffer(); 
        int size = list.size(); 
        for(int i = 0;i<size;i++) 
        { 
            Test test = list.get(i); 
            sb.append("name="+test.getName()); 
            sb.append("|"); 
            sb.append("text="+test.getText()); 
            sb.append("\n"); 
        } 
        tv.setText(sb.toString()); 
    } 
    //獲得xml檔案資料,並放回一個list 
    public  ArrayList<Test> getXmlData(Context context,int id) 
    { 
        ArrayList<Test> list = new ArrayList<Test>(); 
          try { 
              //開啟文檔 
                XmlResourceParser xml = context.getResources().getXml(id); 
                int eventType = xml.next(); 
                try { 
                    // 切換到下一個狀態,並獲得目前狀態的類型 
                    Test test = null; 
                    while ( true )  
                    { 
                         
                        if(test == null) 
                        { 
                            test = new Test(); 
                        } 
                       // 文檔開始狀態 
                       if (eventType == XmlPullParser. START_DOCUMENT )  
                       { 
                       } 
                       // 標籤開始狀態 
                       else if (eventType == XmlPullParser. START_TAG )  
                       { 
                          // 將標籤名稱和當前標籤的深度(根節點的 depth 是 1 ,第 2 層節點的 depth 是 2 ,類推) 
                          switch (xml.getDepth()) 
                          { 
                          case 1: 
                             break ; 
                          case 2: 
                              //獲得屬性name的值 
                              /*
                               * <tests>
                                    <test name="test_1">測試1</test>
                                 </tests>
                               */ 
                              //從上面可以看出test在tests下面,也是深度為2 
                             test.setName(xml.getAttributeValue(null,"name")); 
                              break; 
                          } 
                       } 
                       // 讀取標籤內容狀態 
                       else if (eventType == XmlPullParser. TEXT )  
                       { 
                           //<test name="test_1">測試1</test>獲得標籤中的資料 
                           String str = xml.getText(); 
                           if(str!=null&amp;&amp;!str.equals("")) 
                           { 
                               test.setText(str); 
                               list.add(test); 
                               test = null; 
                           } 
                       } 
                       else if (eventType == XmlPullParser. END_TAG )  
                       { 
                           //結束標籤 
                       } 
                       // 文檔結束狀態 
                       else if (eventType == XmlPullParser. END_DOCUMENT )  
                       { 
                          // 文件剖析結束後,退出 while 迴圈 
                          break ; 
                       } 
                       // 切換到下一個狀態,並獲得目前狀態的類型 
                       eventType = xml.next(); 
              
                    } 
                 } catch (Exception e) { 
                    e.printStackTrace(); 
                 } 
            } catch (Exception e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
          return list; 
    } 
    //Test類 
    class Test 
    { 
        private String name;//name屬性 
        private String text;//內容 
        public String getName() { 
            return name; 
        } 
        public void setName(String name) { 
            this.name = name; 
        } 
        public String getText() { 
            return text; 
        } 
        public void setText(String text) { 
            this.text = text; 
        } 
         
    } 

作者“漢堡的部落格”
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.