码迷,mamicode.com
首页 > 移动开发 > 详细

XmlPullParser(安卓推荐)

时间:2016-02-25 22:56:19      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

添加xml文件XmlPullParser

package com.it.heima.review;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;

import org.xmlpull.v1.XmlSerializer;

import android.R.xml;
import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Xml;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText ed_ssname;
    private RadioGroup rgb;
    private Button btnButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnButton = (Button) findViewById(R.id.btnSave);
        ed_ssname = (EditText) findViewById(R.id.ed_name);
        rgb = (RadioGroup) findViewById(R.id.rgb);
        btnButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String studentname = ed_ssname.getText().toString().trim();
                String sex;
                if (TextUtils.isEmpty(studentname)) {
                    Toast.makeText(getApplicationContext(), "ssss", 0);
                } else {
                    int id = rgb.getCheckedRadioButtonId();

                    if (id == R.id.male) {
                        sex = "男";
                    } else {
                        sex = "女";
                    }
                    File file = new File(getFilesDir(), studentname + ".xml");
                    try {
                        OutputStream out = new FileOutputStream(file);
                        XmlSerializer serializer = Xml.newSerializer();
                        serializer.setOutput(out, "utf-8");
                        serializer.startDocument("utf-8", true);
                        serializer.startTag(null, "name");
                        serializer.text(studentname);
                        serializer.endTag(null, "name");
                        serializer.startTag(null, "sex");
                        serializer.text(sex);
                        serializer.endTag(null, "sex");
                        serializer.endDocument();
                        Toast.makeText(getApplicationContext(), "sucess", 0);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}

解析

package com.itheima.pullparser;

import java.io.IOException;
import java.io.InputStream;

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

import com.itheima.pullparser.domain.Product;

import android.os.Bundle;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //使用 xml  pull 解析器 去 解析 xml 文件的内容
        
        // Xml
        XmlPullParser pullParser = Xml.newPullParser();
        
        try {
            InputStream in = getAssets().open("result.xml");
            
            // 解析的源 是什么 ???  --- result.xml
            pullParser.setInput(in, "gbk");
            
            //获得 一个 事件的 类型 
            int eventType = pullParser.getEventType();
/*
<?xml version="1.0" encoding="gbk"?>
<smartresult>
  <product type="mobile">
    <phonenum>13512345678</phonenum>
    <location>重庆移动神州行卡</location>
    <phoneJx>有得有失,华而不实,须防劫财,始保平安 吉带凶</phoneJx>
 </product>
</smartresult>
 *             
 */
            Product p=null;
            while(eventType!=XmlPullParser.END_DOCUMENT){
                
                if(eventType==XmlPullParser.START_TAG){
                    
                    //判断是否是 元素的开始 , 只要是某个 元素的开始位置, 那么就会进入这里 
                    //获得 当前解析到的元素的名称
                    if("product".equals(pullParser.getName())){
                        p = new Product();
                        // sax 解析 
                        
                        //准备 product 类的一个实例 , 去 封装数据
                        String type = pullParser.getAttributeValue(0);
                        p.setType(type);
                    }else if("phonenum".equals(pullParser.getName())){
                        
                        //获得   13512345678
                        
                        // <phonenum>13512345678</phonenum>
                        String phonenum = pullParser.nextText();
                        p.setPhonenum(phonenum);
                    }else if("location".equals(pullParser.getName())){
                        
                        //<location>重庆移动神州行卡</location>
                        String location = pullParser.nextText();
                        p.setLocation(location);
                    }else if("phoneJx".equals(pullParser.getName())){
                        
//                        <phoneJx>有得有失,华而不实,须防劫财,始保平安 吉带凶</phoneJx>
                        String phoneJx = pullParser.nextText();
                        pullParser.getText();
                        p.setPhoneJx(phoneJx);
                    }
                }
                //手动 挪动 "指针 "
                eventType = pullParser.next();
            }
            
            if(p!=null){
                System.out.println(p.toString());
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
    }

}

 

XmlPullParser(安卓推荐)

标签:

原文地址:http://www.cnblogs.com/wswbk/p/5218447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!