How to use SAX parse xml with the same tag and multi-attribute
When I use SAX to parse this XML,I don't know how to get this three "link" tag's href attribute开发者_JAVA技巧s value by the three different "rel" attributes.
<entry>
<title>ahbei</title>
<link href="http://api.douban.com/people/1000001" rel="self" />
<link href="http://www.douban.com/people/ahbei/" rel="alternate" />
<link href="http://img3.douban.com/icon/u1000001-20.jpg" rel="icon" />
<uri>http://api.douban.com/people/1000001</uri>
</entry>
I can't get the "http://img3.douban.com/icon/u1000001-20.jpg" by this code.
public void startElement(String uri, String localName, String qName,
Attributes atts) throws SAXException {
if (localName.equals("link") && (atts.getValue("rel") == "icon")) // (*)
{
NowState = DBU_icon_url;
DouBanUser.setIcon(atts.getValue(0));
return;
}
}
Thank you.
change
"if (localName.equals("link") && (atts.getValue("rel") == "icon"))
"
=>
"if (localName.equals("link") && atts.getValue("rel").equals("icon"))
"
I think this will solve your problem.
NodeList link = elementW.getElementsByTagName("link");
int getLinkCount = link.getLength();
Element elementLink;
if (getLinkCount != 0) {
for (int i = 0; i < getLinkCount; i++) {
elementLink = (Element) Link.item(i);
// Here you can get these three links.
}
}
精彩评论