XmlPullParser - 只为&QUOT期待;&LT;标题&GT;&QUOT;标签,但结果返回&QUOT;&LT;媒体:标题&GT;&QUOT;太 [英] XmlPullParser - looking only for "<title>" tag, but result returned for "<media:title>" too
问题描述
我试图解析使用XmlPullParser寻找℃的RSS提要,标题方式&gt;
标签,如果标签被发现,那里面提取文本< BR>
一切为了简单的RSS文件的伟大工程,但如果RSS源中包含类似这样:&LT;媒体:标题类型=HTML&GT;
,该行被处理了。
然而,这不是预期的结果,我只是在寻找&LT;标题&GT;
,以及&LT;媒体:标题类型= HTML方式&gt;
我的问题是我怎么能修改我的code实现这一目标,还是什么我在这里丢失?
{尝试 网址URL =新的URL(HTTP://androidresearch.word$p$pss.com/feed/); XmlPullParserFactory工厂= XmlPullParserFactory.newInstance();
factory.setNamespaceAware(真);
XmlPullParser XPP = factory.newPullParser(); xpp.setInput(的getInputStream(URL),UTF_8); INT EVENTTYPE = xpp.getEventType();
而(EVENTTYPE!= XmlPullParser.END_DOCUMENT){ 如果(EVENTTYPE == XmlPullParser.START_TAG){
如果(xpp.getName()。equalsIgnoreCase(标题)){
items.add(xpp.nextText());
}否则如果(xpp.getName()。equalsIgnoreCase(链接)){
// Log.d(GREC,xpp.nextText());
links.add(xpp.nextText());
}
} EVENTTYPE = xpp.next();
} }赶上(MalformedURLException的E){
e.printStackTrace();
}赶上(XmlPullParserException E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
或者你可以关闭在<命名空间意识href=\"http://developer.android.com/reference/org/xmlpull/v1/XmlPullParserFactory.html#setNamespaceAware%28boolean%29\"相对=nofollow>工厂,或者您可以检查命名空间通过的 getNamespace 和的 NO_NAMESPACE
I'm trying to parse an rss feed using the XmlPullParser looking for the <title>
tag, if the tag is found, then extract the text inside.
Everything works great for simple rss files, but if the rss feed contains something like this: <media:title type="html">
, the line is processed too.
However this is not the expected result, I'm looking only for <title>
, and for <media:title type="html">
.
And my question is how can I modify my code to achieve this, or what am I missing here?
try {
URL url = new URL("http://androidresearch.wordpress.com/feed/");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("title")) {
items.add(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("link")) {
// Log.d("GREC", xpp.nextText());
links.add(xpp.nextText());
}
}
eventType = xpp.next();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Either you can turn off namespace awareness in the factory, or you can check that the namespace actually is empty via getNamespace and NO_NAMESPACE
这篇关于XmlPullParser - 只为&QUOT期待;&LT;标题&GT;&QUOT;标签,但结果返回&QUOT;&LT;媒体:标题&GT;&QUOT;太的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!