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(安卓)MapView 申请apiKey
  3. 开机引导程序只执行一次的方法
  4. Android中onContextItemSelected不响应
  5. Android(安卓)MapView 申请apiKey
  6. Android(安卓)Volley库源码简析(HTTP Request部分)
  7. Android(安卓)MapView 申请apiKey
  8. Android中使用Gson解析JSON数据
  9. Android中使用Gson解析JSON数据

随机推荐

  1. android高级应用课程
  2. android (4)控件的xml属性
  3. android adb 命令
  4. 【Android布局】在程序中设置android:gra
  5. 【Android布局】在程序中设置android:gra
  6. Android(安卓)Studio安装以及Fetching an
  7. Android工具箱之理解app资源文件
  8. Android的网站资源收集大全
  9. Android(安卓)动画标签——rotate
  10. Android音频开发(7):音乐可视化-FFT频谱图