• android——使用pull解析xml文件


    1、persons.xml

       将persons.xml文件放到src目录下。其代码如下:

    <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
    <persons>
    	<person id="1">
    		<name>范冰冰</name>
    		<age>31</age>
    	</person>
    	<person id="2">
    		<name>林志玲</name>
    		<age>38</age>
    	</person>
    	<person id="3">
    		<name>章泽天</name>
    		<age>20</age>
    	</person>
    </persons>


    2、Person

    新建一个名叫Person的javabean,代码如下:

    package com.njupt.xml1;
    
    public class Person {
    
    	private Integer id;
    	private String name;
    	private Integer age;
    	public Integer getId() {
    		return id;
    	}
    	public void setId(Integer id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	@Override
    	public String toString() {
    		return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";
    	}
    	
    	
    	public Person() {
    		super();
    	}
    	
    	
    	public Person(Integer id, String name, Integer age) {
    		super();
    		this.id = id;
    		this.name = name;
    		this.age = age;
    	}
    	
    	
    	
    }
    


    3、PersonService

        在这里使用android中已经集成的pull来解析xml文件,代码如下:

    package com.njupt.xml1;
    
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    
    import android.util.Xml;
    
    public class PersonService {
    
    	public List<Person> loadPersons(InputStream in) throws Exception{
    		XmlPullParser parser = Xml.newPullParser();
    		parser.setInput(in,"UTF-8");
    		
    		List<Person> persons = new ArrayList<Person>();
    		Person p = null;
    		
    		for(int type = parser.getEventType() ; type != XmlPullParser.END_DOCUMENT; type = parser.next()){
    			if( type == XmlPullParser.START_TAG){
    				if( parser.getName().equals("person")){
    					p = new Person();
    					String id = parser.getAttributeValue(0);
    					p.setId(Integer.parseInt(id));
    					
    					persons.add(p);
    				}else if(parser.getName().equals("name")){
    					String name = parser.nextText();
    					p.setName(name);
    					
    				}else if(parser.getName().equals("age")){
    					String age = parser.nextText();
    					p.setAge(Integer.parseInt(age));
    				}
                   
    				
    			}
    		}
    		
    		return persons;
    	}
    	
    }
    


    ==================================================================

    另附上android中测试时,xml中需要配置的两段代码:

     <instrumentation
            android:name="android.test.InstrumentationTestRunner"
            android:targetPackage="com.njupt.xml1" />
        <uses-library android:name="android.test.runner" />


    最后运行的结果是:



       

  • 相关阅读:
    迭代器和生成器
    装饰器进阶
    闭包和装饰器
    函数的嵌套
    函数的参数
    什么是粘包现象
    模拟ssh远程命令执行
    客户端与服务端代码bug修复和加入循环
    用socket实现简单的套接字通讯
    网络编程之客户端和服务端,
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3151302.html
Copyright © 2020-2023  润新知