• android——写xml


    在PersonService的基础上,加上savePersons(·····),这时的PersonService为:

    package com.njupt.xml;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    import org.xmlpull.v1.XmlSerializer;
    
    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;
    	}
    
    	public void savePersons(List<Person> persons,
    			FileOutputStream out) throws Exception {
    
    		XmlSerializer serializer = Xml.newSerializer();
    		serializer.setOutput(out,"UTF-8");
    		
    		serializer.startDocument("UTF-8", true);
    		serializer.startTag(null, "persons");
    		
    		for(Person p : persons){
    			serializer.startTag(null, "person");
    			serializer.attribute(null, "id", p.getId().toString());
    			
    			serializer.startTag(null, "name");
    			serializer.text(p.getName());
    			serializer.endTag(null, "name");
    			
    			serializer.startTag(null, "age");
    			serializer.text(p.getAge().toString());
    			serializer.endTag(null, "age");
    			
    			
    			serializer.endTag(null, "person");
    		}
    		serializer.endTag(null, "persons");
    		serializer.endDocument();
    		
    	}
    }
    


    2、PersonTest

    package com.njupt.xml;
    
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.util.List;
    
    import android.test.AndroidTestCase;
    
    public class PersonTest extends AndroidTestCase{
    
    	public void test1() throws Exception{
    		PersonService service = new PersonService();
    		InputStream in = PersonTest.class.getClassLoader().getResourceAsStream("persons22.xml");
    	    
    		List<Person> persons = service.loadPersons(in);
    	 
    		for(Person p : persons){
    			System.out.println(p);
    		}
    		
    		Person p = new Person(4,"allen",20);
    		persons.add(p);
    		
    		service.savePersons(persons,new FileOutputStream("/mnt/sdcard/persons22.xml"));
    	}
    }
    


    3、Person22.xml

    这时生成的person22.xml文件的内容如下(注意,他不会帮我们把xml文件格式化):

    <?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>26</age></person><person id="4"><name>allen</name><age>20</age></person></persons>



  • 相关阅读:
    java 手写 jvm高性能缓存
    给大家推荐一款非常好用的表单验证插件:lr-verify.js
    如何设计处优秀的Restful API
    volatile、static
    微服务学习(一):微服务介绍
    spark过滤算子+StringIndexer算子出发的一个逻辑bug
    spark和深度学习集成调研
    收藏一个不错的个人博客
    二分法中的逼近法
    netty服务端启动--ServerBootstrap源码解析
  • 原文地址:https://www.cnblogs.com/snake-hand/p/3151237.html
Copyright © 2020-2023  润新知