Android SAXPArser, parsing enclosure tag, how?
I want to parse all enclosure tag, with my SAXPArser开发者_如何学运维, how i can do that?
<enclosure url="http://website.com/picture.jpg" width="120" height="90" type="image/jpeg" length="10000" />
I will point you to a very helpful resource regarding XML parsing as I'm certainly not going to write up the entire parser.
This is an interesting read on parsing XML files in Android which helped me when I started Android development. I would use the SAX parser even tho it might be a bit more complicated at first.
EDIT:
The DefaultHandler
's startElement()
method takes four parameters; the namespace URI, the local name (without prefix), the fully qualified name with the prefix and the attributes of that tag. Which looks like this.
Now in that method if you want to access the contents of a tags attribute you just call the attributes getValue()
method passing it the name of the attribute you want to get the content from as a String like below.
That would be the XML tag.
<MyTag myAttribute="someValue">
And that is the startElement()
method handling it.
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
if(localName.equals("MyTag")) {
String attsValue = atts.getValue("myAttribute");
}
}
Well, I used another approach and I hope it could be useful for who's looking for that answer.
I used a map to store all data.
NodeList nodes = doc.getElementsByTagName(TAG_ITEM_NEWS);
for (int i = 0; i < nodes.getLength(); i++)
{
final HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
// ENCLOSURE:
final NamedNodeMap e_attr = Read_XML.getElementAttributes(e,TAG_ENCLOSURE_NEWS);
map.put(TAG_ENCLOSURE_URL_NEWS, Read_XML.getSingleAttribute(e_attr,TAG_ENCLOSURE_URL_NEWS));
map.put(TAG_ENCLOSURE_TYPE_NEWS, Read_XML.getSingleAttribute(e_attr,TAG_ENCLOSURE_TYPE_NEWS));
map.put(TAG_ENCLOSURE_LENGTH_NEWS, Read_XML.getSingleAttribute(e_attr,TAG_ENCLOSURE_LENGTH_NEWS));
// GUID:
final NamedNodeMap g_attr = Read_XML.getElementAttributes(e,TAG_GUID_NEWS);
map.put(TAG_GUID_ISPERMALINK_NEWS, Read_XML.getSingleAttribute(g_attr,TAG_GUID_ISPERMALINK_NEWS));
// ALL THE OTHERS:
map.put(TAG_TITLE_NEWS, Leggi_XML.getValue(e, TAG_TITLE_NEWS));
map.put(TAG_LINK_NEWS, Leggi_XML.getValue(e, TAG_LINK_NEWS));
map.put(TAG_DESCRIPTION_NEWS, Leggi_XML.getValue(e, TAG_DESCRIPTION_NEWS));
map.put(TAG_DATE_NEWS, Leggi_XML.getValue(e, TAG_DATE_NEWS));
list_news.add(map);
}
Now, the main methods to extract attributes are:
public final static NamedNodeMap getElementAttributes( Element elem, String str ) {
NamedNodeMap attrs;
if( elem != null){
if (elem.getElementsByTagName(str)!=null) {
Node enclosure = elem.getElementsByTagName(str).item(0);
attrs = enclosure.getAttributes();
return attrs;
}
}
return null;
}
And:
public final static String getSingleAttribute( NamedNodeMap attrs, String str ) {
if (attrs!=null) {
Node node = attrs.getNamedItem(str);
String attribute = null;
if (node != null) {
attribute = node.getNodeValue();
return attribute;
}
}
return "";
}
Tags are defined elsewhere as: public static final String TAG_ENCLOSURE_NEWS = "enclosure";
精彩评论