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; } @Override public 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); } @Override public void characters(char[] ch, int start, int length) throws SAXException { buffer.append(ch,start,length); //这个很关键 super.characters(ch, start, length); } @Override public 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。