XML文件为:

<?xml version="1.0" encoding="UTF-8"?><profile><title>Android开发实例基础</title><totalTime>120</totalTime><totalScore>100</totalScore><subjectCount>70</subjectCount><score>0</score><elapsedTime>0</elapsedTime><passingScore>60</passingScore><descriptions>描述</descriptions><matrix>1111111111111111111111111111111111111111111111111111111111111111111111</matrix></profile>


XML对应的Java对象为:

package com.lurencun.chenyoca.exam.entity;public class ProfileObject {/** * 考卷信息 */private TestpaperObject testpaper = null;/** * 题目对错矩阵 */private boolean[] matrix = null;public TestpaperObject getTestpaper() {if(testpaper == null) testpaper = new TestpaperObject();return testpaper;}public void setTestpaper(TestpaperObject testpaper) {this.testpaper = testpaper;}public boolean[] getMatrix() {return matrix;}public void setMatrix(boolean[] matrix) {this.matrix = matrix;}}


把XML转换成Java对象的类:

package com.lurencun.chenyoca.exam.util;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import com.lurencun.chenyoca.exam.entity.ProfileObject;public class XmlToPVO extends DefaultHandler {private ProfileObject po;private StringBuffer buffer = new StringBuffer();private final static String TAG = "1";public ProfileObject getProfile(){return po;}@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {if(localName.equals("profile")){po = new ProfileObject();}super.startElement(uri, localName, qName, attributes);}@Overridepublic void characters(char[] ch, int start, int length)throws SAXException {buffer.append(ch,start,length); //这个很关键super.characters(ch, start, length);}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {boolean cleanBuffer = true;if(localName.equals("profile")){ cleanBuffer = false;}else if(localName.equals("title")){ po.getTestpaper().setTitle(buffer.toString().trim());}else if(localName.equals("totalTime")){ po.getTestpaper().setTotalTime(Integer.parseInt(buffer.toString().trim()));}else if(localName.equals("totalScore")){ po.getTestpaper().setTotalScore(Integer.parseInt(buffer.toString().trim()));}else if(localName.equals("subjectCount")){ po.getTestpaper().setSubjectCount(Integer.parseInt(buffer.toString().trim()));}else if(localName.equals("score")){ po.getTestpaper().setScore(Integer.parseInt(buffer.toString().trim()));}else if(localName.equals("elapsedTime")){ po.getTestpaper().setElapsedTime(Integer.parseInt(buffer.toString().trim()));}else if(localName.equals("passingScore")){ po.getTestpaper().setPassingScore(Integer.parseInt(buffer.toString().trim()));}else if(localName.equals("descriptions")){ po.getTestpaper().setDescription(buffer.toString().trim());}else if(localName.equals("matrix")){char[] mx = buffer.toString().toCharArray();boolean[] matrix = new boolean[mx.length];for(int i=0;i<mx.length;i++){if(TAG.equals(mx[i])){matrix[i] = true;}else{matrix[i] = false;}}po.setMatrix(matrix);}if(cleanBuffer) buffer.setLength(0);super.endElement(uri, localName, qName);}}


在需要执行XML转换的地方执行以下代码:

XmlToTVO xtt = new XmlToTVO(testpaperList); //构造方法是我自己的//file是 File对象实体,是XML文件的对象实体。android.util.Xml.parse(new FileInputStream(file),Xml.Encoding.UTF_8,xml);


Android框架的SAX引擎会自动调用相关方法,把XML中的数据填充到JavaObject中。

更多相关SAX的知识,请自己Google。

更多相关文章

  1. 类和 Json对象
  2. Android(安卓)Bundle类
  3. Android(安卓)MapView 申请apiKey
  4. Android传感器
  5. Android应用程序组件Content Provider在应用程序之间共享数据的
  6. Android(安卓)WebView的使用方法总结
  7. Android(安卓)横竖屏切换
  8. Android(安卓)通过WebView 调用Js sqlite数据库
  9. 初学Android,五大布局对象(六)

随机推荐

  1. android 玩转ContentProvider之二--实现多
  2. android实现在横竖屏切换时页面信息不被
  3. android UI进阶之弹窗的使用
  4. 巨人的崛起 Android操作系统发展历程
  5. Android系统的Binder机制分析
  6. android TextView设置字过多长长度后面显
  7. Android编译环境搭建步骤(公司) - 写给自
  8. android之Binder机制解析
  9. Android创建SDCard镜像
  10. Android中ListView和ScrollView总结【安