要转换的.xml文件
<?xml version="1.0" encoding="gb2312"?>
<LESSON id="0">
<QUESTIONS type="拼音篇">
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>chēn许</STEM>
<OPTION>称</OPTION>
<OPTION>茫</OPTION>
<OPTION>涓</OPTION>
<OPTION>烁</OPTION>
<ANSWER>称</ANSWER>
</QUESTION>
<QUESTION type='2'>
<TITLE>给下列拼音选出正确的汉字。</TITLE>
<STEM>chēn</STEM>
<OPTION>称</OPTION>
<OPTION>贯</OPTION>
<OPTION>脉</OPTION>
<OPTION>怨</OPTION>
<ANSWER>称</ANSWER>
</QUESTION>
<QUESTION type='3'>
<TITLE>给下列汉字选出正确的读音。</TITLE>
<STEM>称</STEM>
<OPTION>chēn</OPTION>
<OPTION>hn</OPTION>
<OPTION>hū</OPTION>
<OPTION>jué</OPTION>
<ANSWER>chēn</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>食yn</STEM>
<OPTION>盐</OPTION>
<OPTION>昂</OPTION>
<OPTION>帝</OPTION>
<OPTION>帷</OPTION>
<ANSWER>盐</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>yn水</STEM>
<OPTION>盐</OPTION>
<OPTION>栽</OPTION>
<OPTION>魄</OPTION>
<OPTION>腾</OPTION>
<ANSWER>盐</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>海yn</STEM>
<OPTION>盐</OPTION>
<OPTION>菌</OPTION>
<OPTION>遇</OPTION>
<OPTION>凌</OPTION>
<ANSWER>盐</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>精yn</STEM>
<OPTION>盐</OPTION>
<OPTION>栽</OPTION>
<OPTION>训</OPTION>
<OPTION>率</OPTION>
<ANSWER>盐</ANSWER>
</QUESTION>
<QUESTION type='2'>
<TITLE>给下列拼音选出正确的汉字。</TITLE>
<STEM>yn</STEM>
<OPTION>盐</OPTION>
<OPTION>嘶</OPTION>
<OPTION>资</OPTION>
<OPTION>茎</OPTION>
<ANSWER>盐</ANSWER>
</QUESTION>
<QUESTION type='3'>
<TITLE>给下列汉字选出正确的读音。</TITLE>
<STEM>盐</STEM>
<OPTION>yn</OPTION>
<OPTION>bó</OPTION>
<OPTION>pin</OPTION>
<OPTION>fú</OPTION>
<ANSWER>yn</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>lǒn罩</STEM>
<OPTION>笼</OPTION>
<OPTION>恐</OPTION>
<OPTION>振</OPTION>
<OPTION>训</OPTION>
<ANSWER>笼</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>lǒn统</STEM>
<OPTION>笼</OPTION>
<OPTION>栽</OPTION>
<OPTION>杈</OPTION>
<OPTION>梢</OPTION>
<ANSWER>笼</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>lǒn络</STEM>
<OPTION>笼</OPTION>
<OPTION>镢</OPTION>
<OPTION>蒙</OPTION>
<OPTION>昏</OPTION>
<ANSWER>笼</ANSWER>
</QUESTION>
<QUESTION type='2'>
<TITLE>给下列拼音选出正确的汉字。</TITLE>
<STEM>lǒn</STEM>
<OPTION>笼</OPTION>
<OPTION>恐</OPTION>
<OPTION>虎</OPTION>
<OPTION>蹲</OPTION>
<ANSWER>笼</ANSWER>
</QUESTION>
<QUESTION type='3'>
<TITLE>给下列汉字选出正确的读音。</TITLE>
<STEM>笼</STEM>
<OPTION>lǒn</OPTION>
<OPTION>dī</OPTION>
<OPTION>hui</OPTION>
<OPTION>shī</OPTION>
<ANSWER>lǒn</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>笼zho</STEM>
<OPTION>罩</OPTION>
<OPTION>封</OPTION>
<OPTION>剧</OPTION>
<OPTION>奥</OPTION>
<ANSWER>罩</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>外zho</STEM>
<OPTION>罩</OPTION>
<OPTION>洪</OPTION>
<OPTION>剩</OPTION>
<OPTION>扶</OPTION>
<ANSWER>罩</ANSWER>
</QUESTION>
<QUESTION type='2'>
<TITLE>给下列拼音选出正确的汉字。</TITLE>
<STEM>zho</STEM>
<OPTION>罩</OPTION>
<OPTION>瞰</OPTION>
<OPTION>凋</OPTION>
<OPTION>义</OPTION>
<ANSWER>罩</ANSWER>
</QUESTION>
<QUESTION type='3'>
<TITLE>给下列汉字选出正确的读音。</TITLE>
<STEM>罩</STEM>
<OPTION>zho</OPTION>
<OPTION>hn</OPTION>
<OPTION>pín</OPTION>
<OPTION>jin</OPTION>
<ANSWER>zho</ANSWER>
</QUESTION>
<QUESTION type='3'>
<TITLE>给下列汉字选出正确的读音。</TITLE>
<STEM>屹</STEM>
<OPTION>yì</OPTION>
<OPTION>nì</OPTION>
<OPTION>zhù</OPTION>
<OPTION>xī</OPTION>
<ANSWER>yì</ANSWER>
</QUESTION>
<QUESTION type='1'>
<TITLE>找出与所给拼音相对应的汉字。</TITLE>
<STEM>dǐn立</STEM>
<OPTION>鼎</OPTION>
<OPTION>峻</OPTION>
<OPTION>格</OPTION>
<OPTION>菌</OPTION>
<ANSWER>鼎</ANSWER>
</QUESTION>
<QUESTION type='2'>
<TITLE>给下列拼音选出正确的汉字。</TITLE>
<STEM>dǐn</STEM>
<OPTION>鼎</OPTION>
<OPTION>糊</OPTION>
<OPTION>昧</OPTION>
<OPTION>扶</OPTION>
<ANSWER>鼎</ANSWER>
</QUESTION>
<QUESTION type='3'>
<TITLE>给下列汉字选出正确的读音。</TITLE>
<STEM>鼎</STEM>
<OPTION>dǐn</OPTION>
<OPTION>yī</OPTION>
<OPTION>yo</OPTION>
<OPTION>zhí</OPTION>
<ANSWER>dǐn</ANSWER>
</QUESTION>
<QUESTION type='2'>
<TITLE>给下列拼音选出正确的汉字。</TITLE>
<STEM>bēn</STEM>
<OPTION>崩</OPTION>
<OPTION>旷</OPTION>
<OPTION>颐</OPTION>
<OPTION>搏</OPTION>
<ANSWER>崩</ANSWER>
</QUESTION>
<QUESTION type='3'>
<TITLE>给下列汉字选出正确的读音。</TITLE>
<STEM>崩</STEM>
<OPTION>bēn</OPTION>
<OPTION>jiǒn</OPTION>
<OPTION>rn</OPTION>
<OPTION>lin</OPTION>
<ANSWER>bēn</ANSWER>
</QUESTION>
</QUESTIONS>
<QUESTIONS type="字词篇">
<QUESTION type='4'>
<TITLE>选出所给字的偏旁部首。</TITLE>
<STEM>潮</STEM>
<OPTION>氵</OPTION>
<OPTION>言</OPTION>
<OPTION>页</OPTION>
<OPTION>艹</OPTION>
<ANSWER>氵</ANSWER>
</QUESTION>
<QUESTION type='5'>
<TITLE>选出能与所给字组词的一项。</TITLE>
<STEM>潮</STEM>
<OPTION>cho女</OPTION>
<OPTION>tn木</OPTION>
<OPTION>非fn</OPTION>
<OPTION>开pì</OPTION>
<ANSWER>cho女</ANSWER>
</QUESTION>
<QUESTION type='4'>
<TITLE>选出所给字的偏旁部首。</TITLE>
<STEM>称</STEM>
<OPTION>禾</OPTION>
<OPTION>刂</OPTION>
<OPTION>竹</OPTION>
<OPTION>月</OPTION>
<ANSWER>禾</ANSWER>
</QUESTION>
<QUESTION type='5'>
<TITLE>选出能与所给字组词的一项。</TITLE>
<STEM>称</STEM>
<OPTION>chēn许</OPTION>
<OPTION>xié调</OPTION>
<OPTION>zhú渐</OPTION>
<OPTION>rón毛</OPTION>
<ANSWER>chēn许</ANSWER>
</QUESTION>
<QUESTION type='4'>
<TITLE>选出所给字的偏旁部首。</TITLE>
<STEM>盐</STEM>
<OPTION>皿</OPTION>
<OPTION>扌</OPTION>
<OPTION></OPTION>
<OPTION>火</OPTION>
<ANSWER>皿</ANSWER>
</QUESTION>
<QUESTION type='5'>
<TITLE>选出能与所给字组词的一项。</TITLE>
<STEM>盐</STEM>
<OPTION>精yn</OPTION>
<OPTION>chēn为</OPTION>
<OPTION>qīn斜</OPTION>
<OPTION>洋yì</OPTION>
<ANSWER>精yn</ANSWER>
</QUESTION>
<QUESTION type='4'>
<TITLE>选出所给字的偏旁部首。</TITLE>
<STEM>笼</STEM>
<OPTION>竹</OPTION>
<OPTION>山</OPTION>
<OPTION>厂</OPTION>
<OPTION>禾</OPTION>
<ANSWER>竹</ANSWER>
</QUESTION>
<QUESTION type='5'>
<TITLE>选出能与所给字组词的一项。</TITLE>
<STEM>笼</STEM>
<OPTION>lǒn络</OPTION>
<OPTION>堡lěi</OPTION>
<OPTION>po球</OPTION>
<OPTION>风mǐ</OPTION>
<ANSWER>lǒn络</ANSWER>
</QUESTION>
<QUESTION type='4'>
<TITLE>选出所给字的偏旁部首。</TITLE>
<STEM>罩</STEM>
<OPTION>罒</OPTION>
<OPTION>目</OPTION>
<OPTION>讠</OPTION>
<OPTION>氵</OPTION>
<ANSWER>罒</ANSWER>
</QUESTION>
<QUESTION type='5'>
<TITLE>选出能与所给字组词的一项。</TITLE>
<STEM>罩</STEM>
<OPTION>外zho</OPTION>
<OPTION>灵hún</OPTION>
<OPTION>退cho</OPTION>
<OPTION>程kòn</OPTION>
<ANSWER>外zho</ANSWER>
</QUESTION>
<QUESTION type='4'>
<TITLE>选出所给字的偏旁部首。</TITLE>
<STEM>蒙</STEM>
<OPTION>艹</OPTION>
<OPTION></OPTION>
<OPTION>广</OPTION>
<OPTION>辶</OPTION>
<ANSWER>艹</ANSWER>
</QUESTION>
<QUESTION type='5'>
<TITLE>选出能与所给字组词的一项。</TITLE>
<STEM>蒙</STEM>
<OPTION>启mén</OPTION>
<OPTION>zhèn奋</OPTION>
<OPTION>mó杖</OPTION>
<OPTION>uì树</OPTION>
<ANSWER>启mén</ANSWER>
</QUESTION>
<QUESTION type='4'>
<TITLE>选出所给字的偏旁部首。</TITLE>
<STEM>薄</STEM>
<OPTION>艹</OPTION>
<OPTION>扌</OPTION>
<OPTION>穴</OPTION>
<OPTION>宀</OPTION>
<ANSWER>艹</ANSWER>
</QUESTION>
<QUESTION type='5'>
<TITLE>选出能与所给字组词的一项。</TITLE>
<STEM>薄</STEM>
<OPTION>轻bó</OPTION>
<OPTION>rón毛</OPTION>
<OPTION>洗shù</OPTION>
<OPTION>灿ln</OPTION>
<ANSWER>轻bó</ANSWER>
</QUESTION>
</LESSON>
要转换成的数据类型:
==============================GameQuestion
public class GameQuestion {
public int type = 0;
public String TITLE = null;
public String STEM = null;
public List<String> OPTION = new ArrayList<String>();
public String ANSWER = null;
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getTITLE() {
return TITLE;
}
public void setTITLE(String TITLE) {
this.TITLE = TITLE;
}
public String getSTEM() {
return STEM;
}
public void setSTEM(String STEM) {
this.STEM = STEM;
}
public List<String> getOPTION() {
return OPTION;
}
public void setOPTION(List<String> OPTION) {
this.OPTION = OPTION;
}
public String getANSWER() {
return ANSWER;
}
public void setANSWER(String ANSWER) {
this.ANSWER = ANSWER;
}
public void addOption( String oneOption){
OPTION.add(oneOption);
}
@Override
public String toString() {
return "GameQuestion{" +
"type=" + type +
",TITLE='" + TITLE + '\'' +
",STEM='" + STEM + '\'' +
",OPTION=" + OPTION +
",ANSWER='" + ANSWER + '\'' +
'}';
}
}
==============================GameQuestions
public class GameQuestions {
public ArrayList<GameQuestion> questionList = new ArrayList<GameQuestion>();
public String type;
public void setType(String type){
this.type = type;
}
public String getType(){
return this.type;
}
public void setGameQuestions( ArrayList<GameQuestion> questionList){
this.questionList = questionList;
}
public ArrayList<GameQuestion> getGameQuestions(){
return this.questionList;
}
public void add(GameQuestion gameQuestion){
this.questionList.add(gameQuestion);
}
@Override
public String toString() {
return "GameQuestions{" +
"questionList=" + questionList +
",type='" + type + '\'' +
'}';
}
}
==========================================================GameXmlBean
public class GameXmlBean {
String lessonId;
ArrayList<GameQuestions> questionsList;
public String getLessonId() {
return lessonId;
}
public void setLessonId(String lessonId) {
this.lessonId = lessonId;
}
public ArrayList<GameQuestions> getQuestionList() {
return questionsList;
}
public void setQuestionList(ArrayList<GameQuestions> questionsList) {
this.questionsList = questionsList;
}
public void AddsetQuestionList(GameQuestions gameQuestions){
this.questionsList.add(gameQuestions);
}
@Override
public String toString() {
return "GameXmlBean{" +
"lessonId='" + lessonId + '\'' +
",questionsList=" + questionsList +
'}';
}
}
public static GameXmlBean parseXML(Context context,String fileName) throws XmlPullParserException,IOException { GameXmlBean gameXmlBean = new GameXmlBean(); ArrayList<GameQuestions> Questions = new ArrayList<GameQuestions>(); GameQuestion onequestion = null; GameQuestions oneQuestons = null; InputStream in = null; XmlPullParser pullParser = Xml.newPullParser(); try { in = new FileInputStream(new File(fileName)); } catch (Exception e) { e.printStackTrace(); } if (null == in) { Toast.makeText(context,"请检查数据",Toast.LENGTH_SHORT).show(); return null; } try { pullParser.setInput(in,"gb2312"); } catch (XmlPullParserException e) { e.printStackTrace(); } int event = pullParser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_DOCUMENT: Log.d("liudezu","XmlPullParser.START_DOCUMENT"); break; case XmlPullParser.START_TAG: String pullName = pullParser.getName(); if (HEAD_LESSON.equals(pullName)) { String id = new String(pullParser.getAttributeValue("","id")); gameXmlBean.setLessonId(id); Log.d("liudezu",pullName+":id = "+ id); } else if (HEAD_QUESTIONS.equals(pullParser.getName())) { oneQuestons = new GameQuestions(); String Type = new String(pullParser.getAttributeValue(0)); oneQuestons.setType(Type); Log.d("liudezu",HEAD_QUESTIONS +":type = "+ Type); } else if (CONTENT_QUESTION.equals(pullParser.getName())) { onequestion = new GameQuestion(); int type = new Integer(new String(pullParser.getAttributeValue(0))); onequestion.setType(type); Log.d("liudezu",CONTENT_QUESTION +":type = "+ type); } else if (CONTENT_TITLE.equals(pullParser.getName())) { pullParser.next(); String title = pullParser.getText(); onequestion.setTITLE(title); Log.d("liudezu","TITLE:" + title); } else if (CONTENT_STEM.equals(pullParser.getName())) { pullParser.next(); onequestion.setSTEM(pullParser.getText()); Log.d("liudezu","STEM:" + pullParser.getText()); } else if (CONTENT_OPTION.equals(pullParser.getName())) { pullParser.next(); onequestion.addOption(pullParser.getText()); Log.d("liudezu","Option:" + pullParser.getText()); } else if (CONTENT_ANSWER.equals(pullParser.getName())) { pullParser.next(); onequestion.setANSWER(pullParser.getText()); Log.d("liudezu","ANSWER:" + pullParser.getText()); } break; case XmlPullParser.TEXT: break; case XmlPullParser.END_TAG: if (HEAD_QUESTIONS.equals(pullParser.getName())) { Log.d("liudezu","HEAD_QUESTIONS.END_TAG"); Questions.add(oneQuestons); oneQuestons = null; } else if (CONTENT_QUESTION.equals(pullParser.getName())) { Log.d("liudezu","CONTENT_QUESTION.END_TAG"); Log.d("liudezu",onequestion.toString()); oneQuestons.add(onequestion); onequestion = null; }else { Log.d("liudezu","XmlPullParser.END_TAG"); } break; default: Log.d("zcl","parseXML: error"); break; } event = pullParser.next(); Log.d("liudezu","event = "+event); } Log.d("liudezu","pullParser========over"); gameXmlBean.setQuestionList(Questions); return gameXmlBean; }
原文链接:https://www.f2er.com/xml/294334.html