xml在android
2012-07-08 19:27:52 paowuxian1015 阅读数 942

Android解析XML之XmlPullParser

?
public void getXML(String url) throws XmlPullParserException,IOException,URISyntaxException
{
    String xmlString=downloadXML(url); 
     
    XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
     
    factory.setNamespaceAware(true);
     
    XmlPullParser parser=factory.newPullParser();
     
 
     
    parser.setInput(new StringReader(xmlString));
     
    int eventType=parser.getEventType();
     
     List<Forecast_conditions> conditionsList=new ArrayList<Forecast_conditions>();
     Forecast_conditions forecast_conditions=null;
     
     
    while (eventType != XmlPullParser.END_DOCUMENT) {
         
        if (eventType == XmlPullParser.START_DOCUMENT) {
            System.out.println("Start Document");
        }else if (eventType== XmlPullParser.START_TAG) {
            System.out.println("Start Tag "+parser.getName());
             
             
             
             
            if (parser.getName().equals("forecast_conditions"))
            {
                forecast_conditions=new Forecast_conditions();
            }
                 
                if (parser.getName().equals("day_of_week")) {
                    forecast_conditions.setDay_of_week(parser.getAttributeValue(0));
                     
                }else if (parser.getName().equals("low")) {
                     
                    forecast_conditions.setLow(parser.getAttributeValue(0));
                }else if (parser.getName().equals("high")) {
                     
                    forecast_conditions.setHigh(parser.getAttributeValue(0));
                }else if (parser.getName().equals("icon")) {
                     
                    try {
                        forecast_conditions.setIcon(parser.getAttributeValue(0));
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                     
                     
                }else if (parser.getName().equals("condition")) {
                     
                    try {
                         forecast_conditions.setCondition(parser.getAttributeValue(0));
                    } catch(Exception e){
                         
                    }
                     
                    
                     
                     
                }
             
             
             
             
           /*   int size=parser.getAttributeCount();
             
            for (int i = 0; i < size; i++) {
                System.out.println("AttributeName "+parser.getAttributeName(i));
                System.out.println("AttributeValue"+parser.getAttributeValue(i));
            } */
        }else if(eventType== XmlPullParser.END_TAG){
            System.out.println("End Tag "+parser.getName());
            if (parser.getName().equals("forecast_conditions")) {
                conditionsList.add(forecast_conditions);
            }
             
        }else if(eventType==XmlPullParser.TEXT){
            System.out.println("Text "+parser.getText());
        }
         
        eventType=parser.next();
    }
    System.out.println("End Document");
     
     
    bindToListView(conditionsList);
    //XmlUtils.
         
     
}
?
  
?
本实例是不完整实例。 初学Android, 很多地方不规范。  仅供参考。

  

0
0
(请您对文章做出评价)
« 博主前一篇:Android绑定对象到ListView中(知识积累)
» 博主后一篇:windows phone 7 客户端和web的交互(WebBrowser的使用)
posted @ 2011-10-20 22:31 Aaron Ding 阅读(2578) 评论(0编辑 收藏

2015-11-05 14:29:19 u013707304 阅读数 197

逐帧动画

<!--
    根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
    根标签下,通过item标签对动画中的每一个图片进行声明
    android:duration 表示展示所用的该图片的时间长度
 -->
 <!--当图片用-->
<?xml version="1.0" encoding="utf-8"?>
<animation-list
 xmlns:android="http://schemas.android.com/apk/res/android"
  android:oneshot="true"
  >
    <item android:drawable="@drawable/icon1" android:duration="150"></item>
    <item android:drawable="@drawable/icon2" android:duration="150"></item>
    <item android:drawable="@drawable/icon3" android:duration="150"></item>
    <item android:drawable="@drawable/icon4" android:duration="150"></item>
    <item android:drawable="@drawable/icon5" android:duration="150"></item>
    <item android:drawable="@drawable/icon6" android:duration="150"></item>
</animation-list>
  imageView.setImageResource(R.drawable.animation1);
  animationDrawable = (AnimationDrawable)imageView.getDrawable();
                animationDrawable.start();
2018-01-19 18:14:02 zhuxunyuoyi 阅读数 89
# 测试相关概念

好的软件不是开发出来的,是不断测试出来的。

# 根据是否知道源码

黑盒

白盒

<!--more-->

# 根据测试粒度

方法测试

单元测试

集成测试


系统测试

# 根据测试暴力程度

压力 12306
冒烟
monkey

# 单元测试

定义一个类继承AndroidTestCase

在清单文件配置uses-library 和 instrumentation


创建测试工程,测试参数自动配好了。

# 日志猫

log.v(tag,test);

log.d(tag,test);

log.i(tag,test);

log.w(tag,test);

log.e(tag,test);


# 登陆案列

```
public void login(View v){
        
        //[2.1]获取用户名和密码
        String name = et_name.getText().toString().trim();
        String pwd = et_userpassword.getText().toString().trim();
        //[2.2]判断name 和 pwd 是否为空
        if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)) {
            
            Toast.makeText(MainActivity.this, "用户名或密码不能为空", 1).show();
            
        }else {
            //[2.3]进行登录的逻辑
            System.out.println("连接服务器 进行登录 等我们讲到 第四天 网络 编程 在说");
        
            if (cb_ischeck.isChecked()) {
                //[2.4]把用户名和密码的数据给我存起来
                boolean result = UserInfoUtils.saveInfo(name, pwd);
                if (result) {
                    Toast.makeText(MainActivity.this, "保存成功", 1).show();
                    
                }else{
                    Toast.makeText(MainActivity.this, "保存失败", 1).show();
                }
                    
                
                
            }else {
                Toast.makeText(MainActivity.this, "请勾选cb", 1).show();
                
            }
            
            
        }
```

# SharedPreferences介绍

SharedPreferences类是一个Android存储数据的常用xml文件类。


## 获取sp实例

```
//[0] 先初始化 sp 的实例
/**
* name 会帮助我们生成一个xml文件
* mode 模式
*/
sp = getSharedPreferences("config", 0);
```

## 获取编辑器

```
//[2.5]获取sp的编辑器
Editor edit = sp.edit();
```

## 存数据

```
edit.putString("name", name);
edit.putString("pwd", pwd);
```


## 最后commit

```
//[2.6]记得把edit 进行提交
edit.commit();
```

# xml序列化

## StingBuffer 组拼
```
//[1]创建sb对象
StringBuffer sb = new StringBuffer();
    
//[2]开始组拼xml文件头
sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
//[3]开始组拼xml根节点
sb.append("<smss>");
//[4]开始组拼sms节点
for (Sms sms : smsLists) {
    sb.append("<sms>");
        
    //[5]开始组拼address节点
    sb.append("<address>");
    sb.append(sms.getAddress());
    sb.append("</address>");
            

    //[6]开始组拼body节点
    sb.append("<body>");
    sb.append(sms.getBody());
    sb.append("</body>");
            

    //[7]开始组拼date节点
    sb.append("<date>");
    sb.append(sms.getDate());
    sb.append("</date>");
            
            
    sb.append("</sms>");
    }
        
    sb.append("</smss>");
        
    //[8]把数据保存到sd卡中
    try {
        File file = new File(Environment.getExternalStorageDirectory().getPath(),"smsbackup.xml");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(sb.toString().getBytes());
        fos.close();//关闭流
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
```

## 使用xmlSerializer序列化生成器

* 获取xmlSerializer实例
```
//[1]获取XmlSerializer 类的实例 通过Xml这个工具类去获取
XmlSerializer serializer = Xml.newSerializer();
```

* 设置xmlSerializer序列化参数

```
//[2]设置 xmlserializer序列化器参数
    File file = new File(Environment.getExternalStorageDirectory().getPath(),"smsbackup2.xml");
    FileOutputStream fos = new FileOutputStream(file);
    serializer.setOutput(fos, "utf-8");

```

* 开始写xml开头
```
//[3]开始写xml文档开头
serializer.startDocument("utf-8", true);
```


* 写xml根节点

```
//[4]写 xml的根节点 namespace 命名空间
serializer.startTag(null, "smss");
```

* 循环写其他节点
```
//[5]循环写 sms节点
            for (Sms sms : smsLists) {
                serializer.startTag(null, "sms");

                // [6]开始address节点
                serializer.startTag(null, "address");
                serializer.text(sms.getAddress());
                serializer.endTag(null,"address");
                
                // [7]开始body节点
                serializer.startTag(null, "body");
                serializer.text(sms.getBody());
                serializer.endTag(null,"body");
                
                // [8]开始date节点
                serializer.startTag(null, "date");
                serializer.text(sms.getDate());
                serializer.endTag(null,"date");
                
                //sms节点结束
                serializer.endTag(null, "sms");
                
            }
```

* 写文档结尾

```
serializer.endTag(null, "smss");
            
//写文档结尾
serializer.endDocument();
fos.close();//关闭流
```

# xml解析

xml来源于服务器,Android开发人员需要做解析,服务器以流的形式返回数据。


## 解析步骤

* 获取xmlPullParser实例
```
//[1]获取XmlPullParser 解析的实例
XmlPullParser parser = Xml.newPullParser();
```

* 设置xmlPullParser参数

```
//[2]设置XmlPullParser 的参数
parser.setInput(in, "utf-8");
```

* 获取解析文档的事件类型


```
//[3]获取事件类型
int type = parser.getEventType();
```

* 具体判断一下解析的哪个标签

```

while(type!= XmlPullParser.END_DOCUMENT){
            
            switch (type) {
            case XmlPullParser.START_TAG: //解析开始标签
                //[4]具体判断一下 解析到是哪个开始标志
                if ("weather".equals(parser.getName())) {
                    //[5]创建一个集合对象
                    weatherLists = new ArrayList<Channel>();
                    
                }else if("channel".equals(parser.getName())){
                    //[6]创建Channel对象
                    channel = new Channel();
                    //[7]获取id值
                    String id = parser.getAttributeValue(0);
                    channel.setId(id);
                    
                }else if("city".equals(parser.getName())){
                    //[8]获取city的数据
                    String city = parser.nextText();
                    channel.setCity(city);
                    
                }else if("temp".equals(parser.getName())){
                    //[8]获取city的数据
                    String temp = parser.nextText();
                    channel.setTemp(temp);
                    
                    
                }else if("wind".equals(parser.getName())){
                    //[8]获取city的数据
                    String wind = parser.nextText();
                    channel.setWind(wind);
                    
                }else if("pm250".equals(parser.getName())){
                    //[8]获取city的数据
                    String pm250 = parser.nextText();
                    channel.setPm250(pm250);
                }
                
                break;

            case XmlPullParser.END_TAG: //解析结束标志
                //判断要解析的结束标签
                if ("channel".equals(parser.getName())) {
                    //把javabean对象存到集合中
                    weatherLists.add(channel);
                    
                }
                
                break;
            }
            
            //不停的向下解析
            type = parser.next();
            
            
        }
```



2012-06-07 17:16:19 anzhu_111 阅读数 484

android:duplicateParentState

如果设置此属性,将直接从父容器中获取绘图状态(光标,按下等)。 注意仅仅是获取绘图状态,而没有获取事件,也就是你点一下LinearLayout时Button有被点击的效果,但是不执行点击事件。

2016-12-23 15:03:00 weixin_34129145 阅读数 3
ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XmlSerializer serializer = Xml.newSerializer();
        try{
            serializer.setOutput(bos, "UTF-8");
            serializer.startDocument(null, Boolean.valueOf(true));
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
            serializer.startTag(null, "root");
            serializer.startTag(null, "Child1");
            serializer.endTag(null, "Child1");
            serializer.startTag(null, "Child2");
            serializer.attribute(null, "attribute", "value");
            serializer.endTag(null, "Child2");
            serializer.startTag(null, "Child3");
            serializer.text("Some text inside child 3");
            serializer.endTag(null,"Child3");
            serializer.endTag(null,"root");
            serializer.endDocument();
            serializer.flush();

        }catch(Exception e) {
            e.printStackTrace();
        }
        String data = bos.toString("UTF-8");


 XmlPullParser parser = Xml.newPullParser();
        ByteArrayInputStream inStream = null;
        try {
            UpdateBean updateBean = new UpdateBean();
            inStream = new ByteArrayInputStream(xmlData.getBytes(CHARSET));
            parser.setInput(inStream, CHARSET);
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.END_TAG:
                        String name = parser.getName();
                        if(RESPONSESCODE.equals(name)){
                            updateBean.responsesCode = parser.nextText();
                        }
                        break;
                }
                eventType = parser.next();
            }

转载于:https://my.oschina.net/zdglf/blog/810957

Android Xml

阅读数 428

Android and XML

阅读数 331

Android&&XML

博文 来自: u011248571

android xml

阅读数 234

android xml

阅读数 5

没有更多推荐了,返回首页