android
android

Reputation: 33

get the attribute value in android sax xml parsing

I have to develop one android xml parsing use sax .

This is my xml feed:

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

I got category name and article title..but i didn't get the image..how can i get the image..pls help me...

i have used below code:

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

}

please give me the solution for these...how can i get the image url from thum_image tag alone ...

Upvotes: 0

Views: 936

Answers (1)

Pihhan
Pihhan

Reputation: 808

Use handler for startElement and endElement both of them. I suggest you use Stack as member of parser.

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

If you want only thumb image and nothing more nested, you may set bool in_thumb_image to true from startElement() and false from endElement() if it is qName=="thumb_image". Stack is more generic. For higher levels, you may use tagStack.search();

And off topic. XML is case sensitive, ignoreCase should not be used in my opinion.

Upvotes: 1

Related Questions