2013-02-19 148 views
0

我要開發一個Android的XML解析使用SAX。得到Android的薩克斯XML屬性值解析

這是我的XML提要:

<root> 
<Categories> 
     <Category name="book"> 
    <Articles> 
    <article articleid="170" title="java programming"> 
     <thumb_image> 
    <image url="http://website/var/tmp/thumb_2536_design_pavitra-rajaram_1_thumb_dec2012__forfeed.jpeg"/> 
     </thumb_image> 
     <images> 
     <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> 
     <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> 
     </images> 
     </article> 
    <article articleid="171" title="Android programming"> 
    <thumb_image> 
    <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/> 
    </thumb_image> 
     <images> 
     <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> 
     <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> 
     </images> 
     </article> 
     </Article> 
    </Category> 
    <Category name="Animals"> 
    <Articles> 
      <article articleid="81" title="Dog"> 
      <thumb_image> 
      <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/> 
     </thumb_image> 
     <images> 
     <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> 
      <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> 
     </images> 
      </article> 
     <article articleid="81" title="Cat"> 
     <thumb_image> 
      <image url="http://website/var/tmp/thumb_2220_1_itc-grand-chola-chennai_main_dec2012__forfeed.jpeg"/> 
      </thumb_image> 
      <images> 
      <image url="http://food/dec2012/1_chef-olaf-niemeier_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> 
      <image url="http://food/dec2012/2_chutneys-from-hamburg_main_dec2012.jpg" alttext="Chef Olaf Niemeier"/> 
      </images> 
      </article> 
      </Article> 
      </Category> 
      </Categories> 

我類別名稱和文章title..but我沒有得到image..how可我得到的image..pls幫我...

我用下面的代碼:

public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 
    // reset 
    tempVal = ""; 
    if (qName.equalsIgnoreCase("Category")) { 
     // create a new instance of Laptop 
     laptop = new Laptop(); 
     laptop.setBrand(attributes.getValue("name")); 
    } 
    else if (qName.equalsIgnoreCase("article")) { 
     // create a new instance of Laptop 
     article = new Laptop(); 
     article.setModel(attributes.getValue("title")); 
    } 
    else if (qName.equalsIgnoreCase("thumb_image")) { 

     // create a new instance of Laptop 
     image = new Laptop(); 
     image.setImageURL(attributes.getValue("url")); 
    } 

} 

請給我解決了這些......我怎麼可以從單獨thum_image標籤圖像網址...

+0

你的元素是圖像,而不是thumb_image ... – njzk2 2013-02-19 14:26:23

+0

@ njzk2請閱讀我更新的問題,並給我的解決方案these.i需要的圖像URL單從thumb_image標籤。 – android 2013-02-19 14:36:21

+0

我已閱讀您更新的問題,並且與之前的評論相同。你想在thumb_image訪問的URL,URL時,在圖像 – njzk2 2013-02-19 15:21:49

回答

1

使用兩者的startElement和endElement處理程序。我建議你使用Stack作爲解析器的成員。

private tagStack = new Stack(); 

    public void startElement(String uri, String localName, String qName, 
    Attributes attributes) { 
     ... 
     else if (qName.equals("image")) { 
     if ("thumb_image".equals(tagStack.peek())) { 
      image = new Laptop(); 
      image.setImageURL(attributes.getValue("url")); 
     } 
     } 
     // make sure it is at the end of method 
     tagStack.push(qName); 
    } 

    public void endElement(String uri, String localName, String qName) { 
     ... 
     tagStack.pop(); 
    } 

如果你想只拇指形象而已嵌套,你可以布爾in_thumb_image從的endElement(),如果它是QNAME ==「thumb_image」設置爲從的startElement()真假。堆棧更通用。對於更高的級別,您可以使用tagStack.search();

而且題外話。 XML是區分大小寫的,我認爲不應該使用ignoreCase。包含URL

+0

我得到以下錯誤:語法錯誤令牌「標籤棧」,VariableDeclaratorId此令牌上這些私人標籤棧=新的堆棧()後的預期; line.how我可以解決這些錯誤? – android 2013-02-20 04:17:29