Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2621687
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Android平台

2013-10-20 18:21:26

简单xml解析:

点击(此处)折叠或打开

  1. package com.example.test;

  2. import java.io.IOException;

  3. import org.xmlpull.v1.XmlPullParser;
  4. import org.xmlpull.v1.XmlPullParserException;

  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;

  11. public class TextXmlActivity extends Activity{

  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.test_xml);

  15.         ///< 显示xml内容View控件
  16.         myTextView = (TextView)findViewById(R.id.xmlTv);
  17.         ///< 读取xml按钮控件
  18.         myButton = (Button)findViewById(R.id.xmlBnt);
  19.         ///< 设置按钮单击事件
  20.         myButton.setOnClickListener(new View.OnClickListener(){
  21.             public void onClick(View v) {
  22.                 ///< 计数变量
  23.                 int counter = 0;
  24.                 StringBuilder sb = new StringBuilder("");
  25.                 ///< 创建xml解析器
  26.                 XmlPullParser parser = getResources().getXml(R.xml.test);
  27.                 try {
  28.                     ///< 产生第一个事件
  29.                     int event = parser.getEventType();
  30.                     while (event != XmlPullParser.END_DOCUMENT)
  31.                     {
  32.                         switch (event)
  33.                         {
  34.                         case XmlPullParser.START_TAG:
  35.                             String name = parser.getName();
  36.                             if (name.equals("customer"))
  37.                             {
  38.                                 ++counter;
  39.                                 sb.append("第" + counter + "条客户信息:" + "\n");
  40.                                 sb.append(parser.getAttributeValue(0) + "\n");
  41.                                 sb.append(parser.getAttributeValue(1) + "\n");
  42.                                 sb.append(parser.getAttributeValue(2) + "\n");
  43.                                 sb.append(parser.getAttributeValue(3) + "\n");
  44.                                 Log.i("content", "读取内容");
  45.                             }
  46.                             break;
  47.                         case XmlPullParser.TEXT:
  48.                             break;
  49.                         case XmlPullParser.END_TAG:
  50.                             break;
  51.                         default:
  52.                             break;
  53.                         }
  54.                         event = parser.next();
  55.                     }
  56.                     ///< 显示解析获得的xml文件内容
  57.                     myTextView.setText(sb.toString());
  58.                 } catch (XmlPullParserException e) {
  59.                     // TODO Auto-generated catch block
  60.                     e.printStackTrace();
  61.                 }
  62.                 catch (IOException e) {
  63.                     // TODO Auto-generated catch block
  64.                     e.printStackTrace();
  65.                 }
  66.             }
  67.         });
  68.     }

  69.     ///< 【顺便显示图片】:显示图片 不按比例缩放图片,目标是把图片塞满整个View。
        ImageView img = (ImageView)findViewById(R.id.bitmapView1);
        Drawable dable = getResources().getDrawable(R.drawable.tail);
        img.setScaleType(ImageView.ScaleType.FIT_XY);
        img.setImageDrawable(dable);

  70.     private TextView myTextView;
  71.     private Button myButton;
  72. }
很多人用SAX解析,自定义解析函数...当然还有其他很多解析方式,各自有优点...
阅读(2771) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~