Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • android 解析xml文件

    post by onelong / 2010-9-10 1:09 Friday [android]

    package com.hl;

    import org.xmlpull.v1.XmlPullParser;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class FileXML extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            TextView show = (TextView)findViewById(R.id.show);
           
            XmlPullParser parser = this.getResources().getXml(R.xml.person);//获取资源
           
            StringBuffer sb = new StringBuffer();
            //类似sax的处理方法
            try {
       while(parser.next() != XmlPullParser.END_DOCUMENT){
        String name = parser.getName();
        if(name != null && name.equals("person")){
         int size = parser.getAttributeCount();//获取元素总长度
         for(int i = 0; i< size ;i++){
          String arrName = parser.getAttributeName(i);//获取元素
          String arrValue = parser.getAttributeValue(i);//获取值
          if(arrName != null && arrName.equals("name"))
           sb.append(arrValue);
         }
        }
       }
       show.setText(sb.toString());
      } catch (Exception e) {
       e.printStackTrace();
      }
        }
    }

    引用地址:
     

    我要评论