android sax not parsing “dc:creator”?

核能气质少年 提交于 2019-12-11 04:16:47

问题


Hope someone can provide some guidance - i've been using android sax parser with several feeds. Now when I want to parse an item that contains following:

<category>category</category>
<dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">creator</dc:creator>
<dc:date xmlns:dc="http://purl.org/dc/elements/1.1/">2011-10-13T07:00:15Z</dc:date>
<geo:lat>51.95271682739258</geo:lat>
<geo:long>-0.21096819639205933</geo:long>

I can get the value of "category" tag, but nothing for the others.

The code used follows examples from http://www.ibm.com/developerworks/opensource/library/x-android/, in particular snippet to parse the tags:

Element item = channel.getChild("item");

item.getChild("category").setEndTextElementListener(new EndTextElementListener() {
  public void end(String body) {
   Log.e("mydebug","cat>>"+body+"<");
  }
});
item.getChild("dc:creator").setEndTextElementListener(new EndTextElementListener() {
  public void end(String body) {
   Log.e("mydebug","creator>>"+body+"<");
  }
});

generates this output:

10-13 14:13:12.763: ERROR/mydebug(609): cat>>category<
10-13 14:13:12.763: ERROR/mydebug(609): cat>>category2<

I'm using the following:

import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Log;
import android.util.Xml;

Anything I'm missing? Only thing I can guess is that ":" in the tag is giving me problems, but I couldn't find any examples of similar issue.

Many thanks for reading.


回答1:


When using the startElement() and endElement() methods in your DefaultHandler, both the local name and qualified name values are passed in. Make sure you're checking the qualified name for things like 'dc:creator' or else they won't match.

See the official documentation here: http://developer.android.com/reference/org/xml/sax/helpers/DefaultHandler.html#startElement%28java.lang.String,%20java.lang.String,%20java.lang.String,%20org.xml.sax.Attributes%29

Also, you may consider using the XML Pull Parser as it's a bit more light weight than SAX and might be more appropriate for your needs: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParserFactory.html




回答2:


You are using an API that only looks at the localname of the element: Android Documentation

Try this instead:

item.getChild("creator").setEndTextElementListener(new EndTextElementListener() {
  public void end(String body) {
   Log.e("mydebug","creator>>"+body+"<");
  }
});


来源:https://stackoverflow.com/questions/7755859/android-sax-not-parsing-dccreator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!