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. Android 读取doc文件
  2. Android中通过资源文件获取drawable的几种方法
  3. android- activity,Application,activity渲染xml文件
  4. Android 文件操作大全
  5. android中加载assets中的资源文件
  6. 初学Android,五大布局对象(六)

随机推荐

  1. android 广播传参数
  2. Android(安卓)实现滑动的六种方式
  3. android之获取信息终端
  4. Android(安卓)日期时间选择控件
  5. android如何让webview里的资源访问本地缓
  6. android自动更新升级APP
  7. Android(安卓)来电翻转静音实现源码
  8. android 获取设备真实ip地址
  9. anroid 7.1添加自定义api服务
  10. Android圆角自定义View