如何使用具有相同标记和多属性的SAX解析xml

时间:2011-02-28 12:38:18

标签: android xml sax

当我使用SAX解析这个XML时,我不知道如何通过三个不同的“rel”属性获得这三个“link”标签的href属性值。

<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>

我无法通过此代码获得“http://img3.douban.com/icon/u1000001-20.jpg”。

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;
        }

    }

谢谢。


变化 “if (localName.equals("link") && (atts.getValue("rel") == "icon"))” =&GT; “if (localName.equals("link") && atts.getValue("rel").equals("icon"))

1 个答案:

答案 0 :(得分:0)

我认为这可以解决您的问题。

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.
}
}
相关问题