因为是测试,所以是先从SD卡读取一个xml文件,转换成对象,再把这个对象生成xml文件,写入SD卡,(若是从服务器获取的,就可以直接使用返回的字符串了)
另外需要注意的问题就是编码,
先发测试xml文件。
/sdcard/ceshi2.xml
<?xml version="1.0" encoding="utf-8"?> <LETTERINFOLIST> <ROWS> <APPEALBASE> <ID>20120726131235</ID> <USER_ID>2012072400010</USER_ID> <APPEAL_NUM>2012072600007</APPEAL_NUM> <APPEAL_RANDOM>7825</APPEAL_RANDOM> <CPTYPE>1</CPTYPE> <CPNAME>某某公司</CPNAME> <APPEAL_THEME>信件主题测试</APPEAL_THEME> <APPEAL_CONTONT>来信内容,测试</APPEAL_CONTONT> <APPEAL_TRANS_STATE>9</APPEAL_TRANS_STATE> <APPEAL_EXTRA>附件1;附件2;附件3</APPEAL_EXTRA> <APPEAL_FEEDBACK_INFO>test1</APPEAL_FEEDBACK_INFO> <ANNET_WHEN_ENDCASE>test2</ANNET_WHEN_ENDCASE> </APPEALBASE> <APPEALBASE> <ID>20120726132134</ID> <USER_ID>2012072400010</USER_ID> <APPEAL_NUM>2012072600008</APPEAL_NUM> <APPEAL_RANDOM>4453</APPEAL_RANDOM> <CPTYPE>1</CPTYPE> <CPNAME>某某公司</CPNAME> <APPEAL_THEME>信件主题测试</APPEAL_THEME> <APPEAL_CONTONT>来信内容,测试</APPEAL_CONTONT> <APPEAL_TRANS_STATE>9</APPEAL_TRANS_STATE> <APPEAL_EXTRA>附件1;附件2;附件3</APPEAL_EXTRA> <APPEAL_FEEDBACK_INFO>test3</APPEAL_FEEDBACK_INFO> <ANNET_WHEN_ENDCASE>test4</ANNET_WHEN_ENDCASE> </APPEALBASE> </ROWS> </LETTERINFOLIST>
LetterInfoList.java
package com.zeng.xstream.model; import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("LETTERINFOLIST") public class LetterInfoList { @XStreamAlias("ROWS") private Rows rows; public Rows getRows() { return rows; } public void setRows(Rows rows) { this.rows = rows; } }
package com.zeng.xstream.model; import java.util.ArrayList; import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; import com.thoughtworks.xstream.annotations.XStreamImplicitCollection; @XStreamAlias("ROWS") @XStreamImplicitCollection("letters") public class Rows { @XStreamAlias("APPEALBASE") private List<LetterItem> letters; public List<LetterItem> getLetters() { return letters; } public void setLetters(List<LetterItem> letters) { this.letters = letters; } }
package com.zeng.xstream.model; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicitCollection; @XStreamAlias("APPEALBASE") public class LetterItem { @XStreamAlias("ID") private String ID; private String USER_ID; private String APPEAL_NUM; private String APPEAL_RANDOM; private String CPTYPE; private String CPNAME; private String APPEAL_THEME; private String APPEAL_CONTONT; private String APPEAL_TRANS_STATE; private String APPEAL_FEEDBACK_INFO; private String APPEAL_EXTRA; private String ANNET_WHEN_ENDCASE; public String getID() { return ID; } public void setID(String iD) { ID = iD; } public String getUSER_ID() { return USER_ID; } public void setUSER_ID(String uSER_ID) { USER_ID = uSER_ID; } public String getAPPEAL_NUM() { return APPEAL_NUM; } public void setAPPEAL_NUM(String aPPEAL_NUM) { APPEAL_NUM = aPPEAL_NUM; } public String getAPPEAL_RANDOM() { return APPEAL_RANDOM; } public void setAPPEAL_RANDOM(String aPPEAL_RANDOM) { APPEAL_RANDOM = aPPEAL_RANDOM; } public String getCPTYPE() { return CPTYPE; } public void setCPTYPE(String cPTYPE) { CPTYPE = cPTYPE; } public String getCPNAME() { return CPNAME; } public void setCPNAME(String cPNAME) { CPNAME = cPNAME; } public String getAPPEAL_THEME() { return APPEAL_THEME; } public void setAPPEAL_THEME(String aPPEAL_THEME) { APPEAL_THEME = aPPEAL_THEME; } public String getAPPEAL_CONTONT() { return APPEAL_CONTONT; } public void setAPPEAL_CONTONT(String aPPEAL_CONTONT) { APPEAL_CONTONT = aPPEAL_CONTONT; } public String getAPPEAL_TRANS_STATE() { return APPEAL_TRANS_STATE; } public void setAPPEAL_TRANS_STATE(String aPPEAL_TRANS_STATE) { APPEAL_TRANS_STATE = aPPEAL_TRANS_STATE; } public String getAPPEAL_FEEDBACK_INFO() { return APPEAL_FEEDBACK_INFO; } public void setAPPEAL_FEEDBACK_INFO(String aPPEAL_FEEDBACK_INFO) { APPEAL_FEEDBACK_INFO = aPPEAL_FEEDBACK_INFO; } public String getAPPEAL_EXTRA() { return APPEAL_EXTRA; } public void setAPPEAL_EXTRA(String aPPEAL_EXTRA) { APPEAL_EXTRA = aPPEAL_EXTRA; } public String getANNET_WHEN_ENDCASE() { return ANNET_WHEN_ENDCASE; } public void setANNET_WHEN_ENDCASE(String aNNET_WHEN_ENDCASE) { ANNET_WHEN_ENDCASE = aNNET_WHEN_ENDCASE; } }
@XStreamAlias("ROWS") //标签 @XStreamImplicitCollection("letters")// 这个表示隐式数组,“”里面值 需要跟你定义的变量名相同
XStreamUtil.java
package com.zeng.xstream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.List; import java.util.Map; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class XStreamUtil { private static Map<Class, Class> mClasses = new HashMap<Class, Class>(); private static XStream xStream = new XStream(new DomDriver()); private static Map<String, Class> wClasses = new HashMap<String, Class>(); /** * * @param clsz * 每一层的标签对应的class (注意! 不是每一个标签!) * @param fileName * 文件路径("/sdcard/test.xml") * @param NULL * 暂没用到; * @return 返回的是最外层对象: */ public static Object Xml2Object(List<Class> clsz, String fileName, String NULL) { for (Class cls : clsz) { if (mClasses.containsKey(cls)) continue; mClasses.put(cls, cls); xStream.processAnnotations(cls); } try { // 这个是设置编码,因为可能编码不同会显现乱码,这个应该还没有具体解决乱码问题 BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(fileName), "GBK")); return xStream.fromXML(br); // return xStream.fromXML(new FileReader(new // File(fileName)));//这个是直接读 } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * * @param clsz * : 每一层的标签对应的class * @param str * : xml 解析出来的String * @return 返回的是最外层对象: */ public static Object Xml2Object(List<Class> clsz, String str) { for (Class cls : clsz) { if (mClasses.containsKey(cls)) continue; mClasses.put(cls, cls); xStream.processAnnotations(cls); } return xStream.fromXML(str); } /*--------------------------------------------*/ /* * Xml2Object 与 Object2Xml 的区别在于 * xStream.processAnnotations(cls); * xStream.alias(cls.node, cls.cls); */ /*-------------------------------------------*/ /** * * @param clsz * 这个和上面的不同就是,这个写入的时候需要 标签 和 type(class); 所以定义了一个类,方便点; * @param obj * 这个是读取到的对象 * @return */ public static String Object2Xml(List<XStreamNode> clsz, Object obj) { for (XStreamNode cls : clsz) { if (wClasses.containsKey(cls.node)) continue; wClasses.put(cls.node, cls.cls); xStream.alias(cls.node, cls.cls); } return xStream.toXML(obj); } static class XStreamNode { public XStreamNode(String node, Class cls) { // TODO Auto-generated constructor stub this.node = node; this.cls = cls; } public String node; public Class cls; } }
package com.zeng.xstream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.zeng.xstream.XStreamUtil.XStreamNode; import com.zeng.xstream.model.LetterInfoList; import com.zeng.xstream.model.LetterItem; import com.zeng.xstream.model.Rows; public class XstreamTestActivity extends Activity { private Button read, write; private List<Class> list; private LetterInfoList data; private TextView tv_show; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); read = (Button) findViewById(R.id.read); write = (Button) findViewById(R.id.write); tv_show = (TextView) findViewById(R.id.show); list = new ArrayList<Class>(); list.add(LetterInfoList.class); list.add(Rows.class); list.add(LetterItem.class); read.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Object obj = XStreamUtil.Xml2Object(list, "/sdcard/ceshi2.xml", ""); System.out.println(obj.toString()); data = (LetterInfoList) obj; Toast.makeText(XstreamTestActivity.this, "读取完毕", Toast.LENGTH_SHORT).show(); } }); write.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub XStreamNode item = new XStreamNode("LETTERINFOLIST", LetterInfoList.class); XStreamNode item1 = new XStreamNode("ROWS", Rows.class); XStreamNode item2 = new XStreamNode("APPEALBASE", LetterItem.class); List<XStreamNode> clsz = new ArrayList<XStreamUtil.XStreamNode>(); clsz.add(item); clsz.add(item1); clsz.add(item2); String object2Xml = XStreamUtil.Object2Xml(clsz, data); System.out.println(object2Xml.toString()); tv_show.setText(object2Xml.toString()); FileOutputStream out = null; try { out = new FileOutputStream(new File("/sdcard/mytest.xml")); out.write(object2Xml.getBytes()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/read" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="读取SD卡xml文件" /> <Button android:id="@+id/write" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="写入SD卡xml文件" /> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/show" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="显示文本内容" /> </ScrollView> </LinearLayout>
下面是demo下载连接
http://download.csdn.net/detail/zgf1991/4517168
点击打开链接