一、xml文件放置位置
一般,xml文件放置在assets文件夹下,assets文件夹的位置,项目中xxxx.iml文件中有说明,如下:
<option name="ASSETS_FOLDER_RELATIVE_PATH" value="/src/main/assets"/>
二、xml文件格式
<?xmlversion="1.0" encoding="utf-8"?>
<Questons>
<Question name="hello"length="10">
<_id>1</_id>
<question>1+1=()</question>
<questionA>2</questionA>
<questionB>3</questionB>
<questionC>4</questionC>
<questionD>5</questionD>
<answer>A</answer>
<analysis>null</analysis>
</Question>
<Question name="hi"length="20">
<_id>2</_id>
<question>1+1=()</question>
<questionA>2</questionA>
<questionB>3</questionB>
<questionC>4</questionC>
<questionD>5</questionD>
<answer>A</answer>
<analysis>null</analysis>
</Question>
</Questons>
三、解析步骤
1.创建XmlPullParser
通过XmlPullParserFactory创建
XmlPullParserFactory factory= XmlPullParserFactory.newInstance(); //构造工厂实例
factory.setNamespaceAware(true);//设置xml命名空间为true
XmlPullParser xmlPullParser = factory.newPullParser(); //创建解析对象
2.通过AssetManager类的open()方法将xml文件导入输入流
InputStream is = getAssets().open("question.xml");
3.设置解析器输入流
xmlPullParser.setInput(is,"UTF-8"); //设置输入流和编码方式
4.产生第一个时间
int evtType = xmlPullParser.getEventType();
5.开始解析
while (evtType!= XmlPullParser.END_DOCUMENT){ switch (evtType){ case XmlPullParser.START_TAG: String tagName = xmlPullParser.getName(); Log.e(TAG,tagName); if (tagName.equals("Question")){ int tagCount = xmlPullParser.getAttributeCount(); Log.e(TAG,tagCount+""); String q1 = xmlPullParser.getAttributeName(0); String q2 = xmlPullParser.getAttributeValue(0); Log.e(TAG,q1+q2); String q3 = xmlPullParser.getAttributeName(1); String q4 = xmlPullParser.getAttributeValue(1); Log.e(TAG,q3+q4); } if (tagName.equals("_id")){ String tagId = xmlPullParser.nextText(); Log.e(TAG,tagName+tagId); } if (tagName.equals("question")){ String tagQuestion = xmlPullParser.nextText(); Log.e(TAG,tagName+tagQuestion); } break; case XmlPullParser.END_TAG: Log.e(TAG,"end tag..."); break; } evtType = xmlPullParser.next(); }
四、方法说明
START_DOCUMENT:XmlPullParser 定义的int常量,表示文档开头;
END_DOCUMENT:XmlPullParser 定义的int常量,表示文档结束;
START_TAG:XmlPullParser 定义的int常量,表示标签开始,例如: <Questons>、<Question name="hello" length="10">、<_id>、<question>等;
END_TAG:XmlPullParser 定义的int常量,表示标签开始,例如:</_id>、</question>、</questionA>、</Question>、</Questons>等;
getName():获取标签的名字,返回字符串,例如question;
nextText():获取标签的内容,返回字符串,如果调用成功,则解析器指向END_TAG位置,例如1+1=( );
getAttributeCount():获取当前标签所包含属性的个数;
当标签有子属性时才能使用以下两个方法,如下所示:
<Question name="hello" length="10">
getAttributeName(intindex):获取index处属性名称,比如Question标签下name;
getAttributeValue(intindex): 获取index处属性值,比如Question标签下name的值hello;
五、解析方法代码
public void parseXml(){ XmlPullParserFactory factory = null; InputStream is=null; try { is = getAssets().open("question.xml"); factory= XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xmlPullParser = factory.newPullParser(); xmlPullParser.setInput(is,"UTF-8"); int evtType = xmlPullParser.getEventType(); while (evtType!= XmlPullParser.END_DOCUMENT){ switch (evtType){ case XmlPullParser.START_TAG: String tagName = xmlPullParser.getName(); Log.e(TAG,tagName); if (tagName.equals("Question")){ int tagCount = xmlPullParser.getAttributeCount(); Log.e(TAG,tagCount+""); String q1 = xmlPullParser.getAttributeName(0); String q2 = xmlPullParser.getAttributeValue(0); Log.e(TAG,q1+q2); String q3 = xmlPullParser.getAttributeName(1); String q4 = xmlPullParser.getAttributeValue(1); Log.e(TAG,q3+q4); } if (tagName.equals("_id")){ String tagId = xmlPullParser.nextText(); Log.e(TAG,tagName+tagId); } if (tagName.equals("question")){ String tagQuestion = xmlPullParser.nextText(); Log.e(TAG,tagName+tagQuestion); } break; case XmlPullParser.END_TAG: Log.e(TAG,"end tag..."); break; } evtType = xmlPullParser.next(); } } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); }finally { if (is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }