Pull解析XML文件-采用自定义布局
除了可以使用 SAX和DOM解析XML文件,大家也可以使用Android内置的Pull解析器解析XML文件。 Pull解析器的运行方式与 SAX 解析器相似。它提供了类似的事件,如:开始元素和结束元素事件,使用parser.next()可以进入下一个元素并触发相应事件。事件将作为数值代码被发送,因此可以使用一个switch对感兴趣的事件进行处理。当元素开始解析时,调用parser.nextText()方法可以获取下一个Text类型元素的值。
1.新建pullxml项目  
2.orders.xml文件存放在res–>xml文件夹中
<?xml version="1.0" encoding="UTF-8"?>
<orders>
     <order>
           <good>
               <name>哈尼蛋糕1</name>
               <price>18</price>
               <sum>2</sum>
           </good>
           <iprice>36</iprice>
           <!-- 未消费11   已消费评价 12   评价过的 13    退款中14   已退款 15   -->
           <status>11</status>
     </order>
     <order>
           <good>
               <name>哈尼蛋糕2</name>
               <price>12</price>
               <sum>2</sum>
           </good>
           <iprice>24</iprice>
           <!-- 未消费11   已消费评价 12   评价过的 13    退款中14   已退款 15   -->
           <status>14</status>
     </order>
     <order>
           <good>
               <name>哈尼蛋糕3</name>
               <price>18</price>
               <sum>2</sum>
           </good>
           <iprice>36</iprice>
           <!-- 未消费11   已消费评价 12   评价过的 13    退款中14   已退款 15   -->
           <status>13</status>
     </order>
     <order>
           <good>
               <name>哈尼蛋糕4</name>
               <price>10</price>
               <sum>2</sum>
           </good>
           <iprice>20</iprice>
           <!-- 未消费11   已消费评价 12   评价过的 13    退款中14   已退款 15   -->
           <status>12</status>
     </order>
</orders>
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 
3.在activity_main.xml中添加ListView控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >
    <ListView
        android:id="@+id/lv_orders"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 
4.order_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" >
    <!-- 图片 -->
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:src="@drawable/honey" />
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_weight="1"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lineSpacingExtra="2dp"
            android:maxLines="2"
            android:text="【三店通用】哈尼蛋糕:  10英寸汽车蛋糕1个" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal" >
            <TextView
                android:id="@+id/tv_iprice"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="总价:98" />
            <TextView
                android:id="@+id/tv_sum"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:gravity="right"
                android:text="数量:1" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
            <Button
                android:id="@+id/btn_status"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="评价" />
            <RatingBar
                android:id="@+id/rb_star"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:numStars="3"
                />
            <TextView
                android:id="@+id/tv_status"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="申请退款"
                android:textColor="#ff8c00" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 
5.Goods.Java  
6.Orders.java  
7.XmlService.java用来解析xml文件
package com.example.service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.content.Context;
import android.content.res.XmlResourceParser;
import com.example.domain.Goods;
import com.example.domain.Orders;
import com.example.pullxml.R;
public class XmlService {
   /**
    * 解析指定文件
    *
    * @param context
    * @return
    * @throws XmlPullParserException
    * @throws IOException
    */
   public List<Orders> parserXml(Context context)
         throws XmlPullParserException, IOException {
      List<Orders> data = null;
      // 解析xml文件内容
      // 1.获取XmlResourceParser解析器对象
      XmlResourceParser parser = context.getResources().getXml(R.xml.orders);
      // 2.获取解析的类型
      int eventType = parser.getEventType(); // 类型代表 开始文档 结束文档 开始元素 结束元素 注解
                                    // text 等等
      // 声明订单
      Orders orders = null;
      // 声明订单对应的商品
      Goods good = null;
      // 3.判断解析的类型是否当文档末尾
      while (eventType != parser.END_DOCUMENT) {
         // 获取解析的tagName标签名
         String tagName = parser.getName();
         // 判断类型
         switch (eventType) {
         case XmlPullParser.START_DOCUMENT:
            data = new ArrayList<Orders>();
            break;
         case XmlPullParser.START_TAG:
            if ("order".equals(tagName)) {
               // 创建订单对象
               orders = new Orders();
            } else if ("good".equals(tagName)) {
               // 商品对象
               good = new Goods();
            } else if ("name".equals(tagName)) {
               // 获取文本值
               good.setName(parser.nextText());
            } else if ("price".equals(tagName)) {
               good.setPrice(Double.parseDouble(parser.nextText()));
            } else if ("sum".equals(tagName)) {
               good.setSum(Integer.valueOf(parser.nextText()));
            } else if ("iprice".equals(tagName)) {
               orders.setPrice(Double.valueOf(parser.nextText()));
            } else if ("status".equals(tagName)) {
               orders.setStatue(Integer.valueOf(parser.nextText()));
            }
            break;
         case XmlPullParser.END_TAG:
            if ("order".equals(tagName)) {
               // 加入到订单的集合中
               data.add(orders);
               // 创建订单对象
               orders = null;
            } else if ("good".equals(tagName)) {
               // 加入到订单中
               orders.setGood(good);
               // 商品对象
               good = null;
            }
            break;
         }
         // 解析下一个
         eventType = parser.next();
      }
      return data;
   }
}
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 - 91
 - 92
 - 93
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 - 91
 - 92
 - 93
 
8.OrdersAdapter.java
package com.example.adapter;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.example.domain.Orders;
import com.example.pullxml.R;
public class OrdersAdapter extends BaseAdapter {
   // 显示数据
   private List<Orders> data = null;
   // 上下文
   private Context context;
   // 绑定对象
   private ViewHolder viewHolder;
   // 构造函数
   public OrdersAdapter(Context context, List<Orders> data) {
      this.context = context;
      this.data = data;
   }
   @Override
   public int getCount() {
      // TODO Auto-generated method stub
      return data.size();
   }
   @Override
   public Object getItem(int position) {
      // TODO Auto-generated method stub
      return data.get(position);
   }
   @Override
   public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
   }
   // 返回条目对应View控件对象
   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      // 1.判断convertView是否为null
      if (convertView == null) {
         // 2.创建绑定的对象
         viewHolder = new ViewHolder();
         // 3.把一个布局文件转换成View对象
         convertView = View.inflate(context, R.layout.order_item, null);
         // 4.获取布局文件中的控件对象 赋值给绑定对象对应的属性
         viewHolder.tv_name = (TextView) convertView
               .findViewById(R.id.tv_name);
         viewHolder.tv_iprice = (TextView) convertView
               .findViewById(R.id.tv_iprice);
         viewHolder.tv_sum = (TextView) convertView
               .findViewById(R.id.tv_sum);
         viewHolder.tv_status = (TextView) convertView
               .findViewById(R.id.tv_status);
         viewHolder.btn_status = (Button) convertView
               .findViewById(R.id.btn_status);
         viewHolder.rb_star = (RatingBar) convertView.findViewById(R.id.rb_star);
         // 5.设置标志
         convertView.setTag(viewHolder);
      } else {
         // 6.直接获取viewHolder对象
         viewHolder = (ViewHolder) convertView.getTag();
      }
      // 获取显示的的订单
      Orders o = data.get(position);
      viewHolder.tv_name.setText(o.getGood().getName());
      viewHolder.tv_iprice.setText(o.getPrice() + "");
      viewHolder.tv_sum.setText(o.getGood().getSum() + "");
      // <!-- 未消费11 已消费评价 12 评价过的 13 退款中14 已退款 15 -->
      if (o.getStatue() == 11) {
         // 设置控件是否可见
         viewHolder.rb_star.setVisibility(View.GONE);
         viewHolder.btn_status.setVisibility(View.GONE);
         viewHolder.tv_status.setText("未消费");
      } else if (o.getStatue() == 12) {
         viewHolder.rb_star.setVisibility(View.GONE);
         viewHolder.tv_status.setVisibility(View.GONE);
         viewHolder.btn_status.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               Toast.makeText(context, "点击了评价", 1).show();
               viewHolder.btn_status.setVisibility(View.GONE);
               viewHolder.rb_star.setVisibility(View.VISIBLE);
            }
         });
      } else if (o.getStatue() == 13) {
         viewHolder.rb_star.setVisibility(View.GONE);
         viewHolder.btn_status.setVisibility(View.GONE);
         viewHolder.tv_status.setText("已评价");
      } else if (o.getStatue() == 14) {
         viewHolder.rb_star.setVisibility(View.GONE);
         viewHolder.btn_status.setVisibility(View.GONE);
         viewHolder.tv_status.setText("退款中");
      } else if (o.getStatue() == 15) {
         viewHolder.rb_star.setVisibility(View.GONE);
         viewHolder.btn_status.setVisibility(View.GONE);
         viewHolder.tv_status.setText("已退款");
      }
      return convertView;
   }
   class ViewHolder {
      public TextView tv_name, tv_iprice, tv_sum, tv_status;
      public Button btn_status;
      public ImageView iv_icon;
      public RatingBar rb_star;
   }
}
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 - 91
 - 92
 - 93
 - 94
 - 95
 - 96
 - 97
 - 98
 - 99
 - 100
 - 101
 - 102
 - 103
 - 104
 - 105
 - 106
 - 107
 - 108
 - 109
 - 110
 - 111
 - 112
 - 113
 - 114
 - 115
 - 116
 - 117
 - 118
 - 119
 - 120
 - 121
 - 122
 - 123
 - 124
 - 125
 - 126
 
- 1
 - 2
 - 3
 - 4
 - 5
 - 6
 - 7
 - 8
 - 9
 - 10
 - 11
 - 12
 - 13
 - 14
 - 15
 - 16
 - 17
 - 18
 - 19
 - 20
 - 21
 - 22
 - 23
 - 24
 - 25
 - 26
 - 27
 - 28
 - 29
 - 30
 - 31
 - 32
 - 33
 - 34
 - 35
 - 36
 - 37
 - 38
 - 39
 - 40
 - 41
 - 42
 - 43
 - 44
 - 45
 - 46
 - 47
 - 48
 - 49
 - 50
 - 51
 - 52
 - 53
 - 54
 - 55
 - 56
 - 57
 - 58
 - 59
 - 60
 - 61
 - 62
 - 63
 - 64
 - 65
 - 66
 - 67
 - 68
 - 69
 - 70
 - 71
 - 72
 - 73
 - 74
 - 75
 - 76
 - 77
 - 78
 - 79
 - 80
 - 81
 - 82
 - 83
 - 84
 - 85
 - 86
 - 87
 - 88
 - 89
 - 90
 - 91
 - 92
 - 93
 - 94
 - 95
 - 96
 - 97
 - 98
 - 99
 - 100
 - 101
 - 102
 - 103
 - 104
 - 105
 - 106
 - 107
 - 108
 - 109
 - 110
 - 111
 - 112
 - 113
 - 114
 - 115
 - 116
 - 117
 - 118
 - 119
 - 120
 - 121
 - 122
 - 123
 - 124
 - 125
 - 126
 
9.MainActivity.java 
10.演示效果  
点击评价按钮 
                    
                
                
            
        
浙公网安备 33010602011771号