Android中,把XML文件轉換成Object對象的方法

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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章