开发者

android sax not parsing "dc:creator"?

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</cat开发者_如何学编程egory>
<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.


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


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+"<");
  }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜