pull解析xml记录
这里现在要解析的xml
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<root>
<resultcode>200 </resultcode>
<reason>successed!</reason>
<result>
<sk>... </sk>
<today>
<temperature>13 ℃~18 ℃</temperature>
<weather>阴转多云</weather>
<weather_id>... </weather_id>
<wind>微风</wind>
<week>星期三</week>
<city>成都</city>
<date_y>2015 年11 月18 日</date_y>
<dressing_index>较舒适</dressing_index>
<dressing_advice>建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。</dressing_advice>
<uv_index>最弱</uv_index>
<comfort_index/>
<wash_index>不宜</wash_index>
<travel_index>较适宜</travel_index>
<exercise_index>较适宜</exercise_index>
<drying_index/>
</today>
<future>
<item>
<temperature>13 ℃~18 ℃</temperature>
<weather>阴转多云</weather>
<weather_id>
<fa>02 </fa>
<fb>01 </fb>
</weather_id>
<wind>微风</wind>
<week>星期三</week>
<date>20151118 </date>
</item>
<item>... </item>
<item>... </item>
<item>... </item>
<item>... </item>
<item>... </item>
<item>... </item>
</future>
</result>
<error_code>0 </error_code>
</root>
以下是解析步骤
传入一个xml文件的输入流
/**
* xml之pull解析
*/
public static String changeXML (InputStream in)
{
StringBuilder sb = new StringBuilder();
实例化一个XmlPullParser解析器,注意Xml是在android.util包中的
XmlPullParser parser = Xml.newPullParser();
try
{
将要解析的文件流或者文件字符流放入解析器中,设置流格式,一般都是utf-8
parser .setInput(in , "utf-8" );
得到事件类型
类型这里用到了
文档开始类型:XmlPullParser.START_DOCUMENT 文档结束类型:XmlPullParser.END_DOCUMENT 开始标签类型:XmlPullParser.START_TAG 结束标签类型:XmlPullParser.END_TAG 如图: 通过判断事件类型,我们可以知道当前处于什么地方
int eventType = parser .getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
String currentTagName = parser.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break ;
case XmlPullParser.START_TAG:
if (currentTagName.equals("resultcode" ))
{
sb.append(parser.nextText() + "\n" );
}
以下都差不多 但要注意了其中可能有相同标签套着的 eventType = parser.next();是跳到下一个开始标签或结束标签
if(currentTagName.equals ("reason" ))
{
sb.append (parser.nextText () + "\n" )
}
if(currentTagName.equals ("temp" ))
{
sb.append (parser.nextText () + "\n" )
}
if(currentTagName.equals ("wind_direction" ))
{
sb.append (parser.nextText () + "\n" )
}
if(currentTagName.equals ("wind_strength" ))
{
sb.append (parser.nextText () + "\n" )
}
if(currentTagName.equals ("humidity" ))
{
sb.append (parser.nextText () + "\n" )
}
if(currentTagName.equals ("time" ))
{
sb.append (parser.nextText () + "\n" )
}
if(currentTagName.equals ("today" ))
{
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
eventType = parser.next ()
while(!(eventType == XmlPullParser.END _TAG && currentTagName.equals ("today" )))
{
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
}
currentTagName = parser.getName ()
}
Log.i ("myout" , "currentTagName = " +currentTagName)
if(currentTagName.equals ("item" ))
{
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
sb.append (parser.nextText () + "\n" )
eventType = parser.next ()
}
break
case XmlPullParser.END _TAG:
break
default:
break
}
获取下一个事件类型
eventType = parser. next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb. toString();
}
运行效果图
以下是用一个TextView显示了BufferBuilder对象的最终结果 TextView放在了一个ScrollView中以便滑动,布局就不给出了
我的感受
pull解析xml就是一步步一层层的向下解析 其中遇到个问题,就是数据老是只能解析一小半。最后才发现,原来就在那小半的最后那里我少往下走了一步(指少写了一个eventType = parser.next();),╮(╯▽╰)╭
想说的话
博客坚持写,不管怎样,今后学习了新的东西就在这里记录一下,以便今后回顾,也希望小小笔记能帮助你们 如果内容有什么地方不对,望吐槽,希望大家能一起学习和成长 来一句:尊重他人、耐心、礼遇每一个人