当前位置:编程学习 > XML/UML >>

Android学习笔记(八)之Android 读写xml文件

[java] 
/**
 * XmlTool.java
 * 版权所有(C) 2012 
 * 创建:cuiran 2012-9-19 下午3:39:40
 */ 
package com.wpn.xml; 
 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.io.StringWriter; 
import java.util.ArrayList; 
import java.util.List; 
 
import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlSerializer; 
 
import android.content.Context; 
import android.util.Log; 
import android.util.Xml; 
 
import com.alibaba.fastjson.JSON; 
import com.wpn.constants.Conf; 
import com.wpn.data.UserSessionDic; 
import com.wpn.message.CallMessage; 
 
/**
 * TODO
 * @author cuiran
 * @version TODO
 */ 
public class XmlTool { 
     
    public boolean writeXML(List<CallMessage> users){ 
        boolean flag=false; 
        String str=writeToString(users); 
         
        flag=writeToXml(UserSessionDic.getContext(), str); 
         
        return flag; 
    } 
     
     public String writeToString(List<CallMessage> users){ 
          //实现xml信息序列号的一个对象     
        XmlSerializer serializer=Xml.newSerializer();       
        StringWriter writer=new StringWriter(); 
        try{ 
            //xml数据经过序列化后保存到String中,然后将字串通过OutputStream保存为xml文件            
            serializer.setOutput(writer);                    
            //文档开始         
            serializer.startDocument("utf-8", true);                
            //开始一个节点            
             
            serializer.startTag("", "calls"); 
             serializer.attribute("", "type", "list"); 
              
              
              
            for(CallMessage call :users){ 
                Log.i(Conf.TAG_DUBUG, "call="+JSON.toJSONString(call)); 
                 serializer.startTag("", "call");          
                /**
                 * 加属性
                 */ 
//               serializer.attribute("", "ordernum", call.getOrdernum()); 
                 serializer.startTag("", "ordernum");   
                 serializer.text(call.getOrdernum()); 
                 serializer.endTag("", "ordernum");     
                  
                 serializer.startTag("", "bytime");   
                 serializer.text(call.getBytime()); 
                 serializer.endTag("", "bytime");     
                  
                 serializer.startTag("", "notifytime");   
                 serializer.text(call.getNotifytime()); 
                 serializer.endTag("", "notifytime");     
                  
                 serializer.startTag("", "servertime");   
                 serializer.text(call.getServertime()); 
                 serializer.endTag("", "servertime");     
                  
                  
                 serializer.startTag("", "slocation");   
                 serializer.text(call.getSlocation()); 
               &
补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,