//andorid以pull的方式来读取XML文件
private boolean parseXML3(InputStream inputStream)
{
if (inputStream == null)
return false;
// 获得XmlPullParser解析器
XmlPullParser xmlParser = Xml.newPullParser();
try
{
// 设置编码方式
xmlParser.setInput(inputStream, "utf-8");
// 获得解析到的事件类别,这里有开始文档,结束文档,开始标签,结束标签,文本等等事件。
int evtType = xmlParser.getEventType();
// 一直循环,直到文档结束(从第一个标签开始到最后一个标签结束)
while (evtType != XmlPullParser.END_DOCUMENT)
{
switch (evtType)
{
//捕捉开始标签(任何一个开始的标签)
case XmlPullParser.START_TAG:
String tag = xmlParser.getName();
// 如果是position标签开始,则读取此标签里面的属性值
if (tag.equalsIgnoreCase("position"))
{
Log.i(TAG, "position标签开始");
String alias = xmlParser.getAttributeValue(null,
"alias");
String appId = xmlParser.getAttributeValue(null,
"appId");
String changeTime = xmlParser.getAttributeValue(null,
"changeTime");
String changeType = xmlParser.getAttributeValue(null,
"changeType");
String height = xmlParser.getAttributeValue(null,
"height");
String pId = xmlParser.getAttributeValue(null, "pId");
String width = xmlParser.getAttributeValue(null,
"width");
String count = xmlParser.getAttributeValue(null,
"count");
Log.i(TAG, "alias:" + alias);
Log.i(TAG, "appId:" + appId);
Log.i(TAG, "changeTime:" + changeTime);
Log.i(TAG, "changeType:" + changeType);
Log.i(TAG, "height:" + height);
Log.i(TAG, "pId:" + pId);
Log.i(TAG, "width:" + width);
Log.i(TAG, "count:" + count);
} else if (tag.equalsIgnoreCase("advertise"))
{
Log.i(TAG, "advertise标签开始,不用读取任何值");
}
else if (tag.equalsIgnoreCase("adId"))
{
Log.i(TAG, "adId标签开始");
String adId = xmlParser.nextText().trim();
Log.i(TAG, "adId:" + adId);
}
else if (tag.equalsIgnoreCase("imgurl"))
{
Log.i(TAG, "imgurl标签开始");
String imgurl = xmlParser.nextText().trim();
Log.i(TAG, "imgurl:" + imgurl);
}
else if (tag.equalsIgnoreCase("openType"))
{
Log.i(TAG, "openType标签开始");
String imgurl = xmlParser.nextText().trim();
Log.i(TAG, "openType:" + imgurl);
}
else if (tag.equalsIgnoreCase("url"))
{
Log.i(TAG, "url标签开始");
String url = xmlParser.nextText().trim();
Log.i(TAG, "url:" + url);
}
else if (tag.equalsIgnoreCase("adType"))
{
Log.i(TAG, "adType标签开始");
String adType = xmlParser.nextText().trim();
Log.i(TAG, "adType:" + adType);
}
break;
//捕捉结束标签(任何一个结束的标签)
case XmlPullParser.END_TAG:
if (xmlParser.getName().equalsIgnoreCase("advertise"))
{
Log.i(TAG, "advertise标签结束");
} else if (xmlParser.getName().equalsIgnoreCase("position"))
{
Log.i(TAG, "position标签结束");
}
break;
default:
break;
}
// 如果xml没有结束,则导航到下一个节点
evtType = xmlParser.next();
}
return true;
} catch (XmlPullParserException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
*****************************需要解释的XML文件如下***********************************************
***********http://www.goodboyenglish.com/test/test2.txt?appId=356&positionId=1,2,3,4*************
<?xml version="1.0" encoding="UTF-8" ?>
<result >
<position
alias="本周最佳应用软件"
appId="131"
changeTime="5"
changeType="0"
height="90"
pId="1"
width="220"
count="1">
<advertise >
<adId >
10
</adId>
<imgurl >
/upload/store/topic/ad1.png
</imgurl>
<openType >
0
</openType>
<url >
/client/getTopicAppList.action?topicId=8
</url>
<adType >
1
</adType>
</advertise>
</position>
<position
alias="英语学习应用软件"
appId="132"
changeTime="5"
changeType="0"
height="90"
pId="2"
width="220"
count="1">
<advertise >
<adId >
11
</adId>
<imgurl >
/upload/store/topic/ad2.png
</imgurl>
<openType >
1
</openType>
<url >
/client/getTopicAppList.action?topicId=5
</url>
<adType >
0
</adType>
</advertise>
</position>
<position
alias="愤怒的小鸟"
appId="135"
changeTime="5"
changeType="0"
height="90"
pId="3"
width="220"
count="1">
<advertise >
<adId >
12
</adId>
<imgurl >
/upload/store/topic/ad3.png
</imgurl>
<openType >
0
</openType>
<url >
/niao.txt
</url>
<adType >
1
</adType>
</advertise>
</position>
<position
alias="本周最佳游戏"
appId="138"
changeTime="5"
changeType="0"
height="90"
pId="4"
width="220"
count="1">
<advertise >
<adId >
18
</adId>
<imgurl >
/upload/store/topic/ad4.png
</imgurl>
<openType >
0
</openType>
<url >
/client/getTopicAppList.action?topicId=9
</url>
<adType >
0
</adType>
</advertise>
</position>
</result>