title: sax parsing through url in android
body: <p>I am trying to parse a URL in Android but it gives a null value except category.  Can you help me fix it?</p>
  
  <pre><code>//my main.xml file
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      &gt;
  &lt;TextView  
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"
      /&gt;
  &lt;/LinearLayout&gt;
  
  //sitelist.java
  package com.androidpeople.xml.parsing;
  
  import java.util.ArrayList;
  
  /** Contains getter and setter method for varialbles  */
  public class SitesList {
  
      /** Variables */
      private ArrayList&lt;String&gt; Product = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; Products = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; PriceSet = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; categoryId = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; id= new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; title  = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; description = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; manufacturer = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; url = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; Image   = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; relevancy = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; minPrice  = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; maxPrice    = new ArrayList&lt;String&gt;();
      private ArrayList&lt;String&gt; stores = new ArrayList&lt;String&gt;();
  
  
      /** In Setter method default it will return arraylist 
       *  change that to add  */
  
      public ArrayList&lt;String&gt; getProduct() {
  
          return this.Product;
      }
  
      public void setProduct(String Product) {
          this.Product.add(Product);
      }
  
      public ArrayList&lt;String&gt; getProducts() {
          return this.Products;
      }
  
      public void setProducts(String Products) {
          this.Products.add(Products);
      }
  
  
      public ArrayList&lt;String&gt; getPriceSet() {
          return PriceSet;
      }
  
      public void setPriceSet(String PriceSet) {
          this.PriceSet.add(PriceSet);
      }
  
      public void setcategoryId(String categoryId) {
          this.categoryId.add(categoryId);
      }
      public ArrayList&lt;String&gt; getcategoryId() {
          return categoryId;
      }
      public ArrayList&lt;String&gt; getid() {
          return id;
      }
  
      public void setid(String id) {
          this.id.add(id);
      }
  
      public ArrayList&lt;String&gt; gettitle() {
          return title;
      }
  
      public void settitle(String title) {
          this.title.add(title);
      }
  
      public ArrayList&lt;String&gt; getdescription() {
          return description;
      }
  
      public void setdescription(String description) {
          this.description.add(description);
      }
      public ArrayList&lt;String&gt; getmanufacturer() {
          return manufacturer;
      }
  
      public void setmanufacturer(String manufacturer) {
          this.manufacturer.add(manufacturer);
      }
  
      public ArrayList&lt;String&gt; geturl() {
          return url;
      }
  
      public void seturl(String url) {
          this.url.add(url);
      }
  
      public ArrayList&lt;String&gt; getImage() {
          return Image;
      }
  
      public void setImage(String Image) {
          this.Image.add(Image);
      }
      public ArrayList&lt;String&gt; getrelevancy() {
          return relevancy;
      }
  
      public void setrelevancy(String relevancy) {
          this.relevancy.add(relevancy);
      }
  
      public ArrayList&lt;String&gt; getminPrice() {
          return minPrice;
      }
  
      public void setminPrice(String minPrice) {
          this.minPrice.add(minPrice);
      }
  
      public ArrayList&lt;String&gt; getmaxPrice() {
          return maxPrice;
      }
  
      public void setmaxPrice(String maxPrice) {
          this.maxPrice.add(maxPrice);
      }
      public ArrayList&lt;String&gt; getstores() {
          return stores;
      }
  
      public void setstores(String stores) {
          this.stores.add(stores);
      }
  
      public void setWebsite(String currentValue) {
          // TODO Auto-generated method stub
  
      }  
  
  }
  
  //myxmlhandler.java
  
  package com.androidpeople.xml.parsing;
  
  import org.xml.sax.Attributes;
  
  
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.DefaultHandler;
  
  public class MyXMLHandler extends DefaultHandler {
  
      Boolean currentElement = false;
      String currentValue = null;
      public static SitesList sitesList = null;
  
      public static SitesList getSitesList() {
          return sitesList;
      }
  
      public static void setSitesList(SitesList sitesList) {
          MyXMLHandler.sitesList = sitesList;
      }
  
      /** Called when tag starts ( ex:- &lt;name&gt;AndroidPeople&lt;/name&gt; 
       * -- &lt;name&gt; )*/
      @Override
      public void startElement(String uri, String localName, String qName,
              Attributes attributes) throws SAXException {
  
          currentElement = true;
  
          if (localName.equals("ProductResponse"))
          {
              //** Start *//* 
              sitesList = new SitesList();
          } else if (localName.equals("Products")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("Products");
              System.out.println("Data"+attr);
              sitesList.setProducts(attr);
          }
          else if (localName.equals("PriceSet")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("PriceSet");
              System.out.println("Data"+attr);
              sitesList.setPriceSet(attr);
          }
          else if (localName.equals("Product")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("categoryId");
              System.out.println("Data"+attr);
              sitesList.setcategoryId(attr);
  
                      /** Get attribute value */
                      String attr1 = attributes.getValue("id");
                      System.out.println("Data"+attr);
                      sitesList.setid(attr);
          }else if (localName.equals("title")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("title");
              System.out.println("Data"+attr);
              sitesList.settitle(attr);
  
  
          }else if (localName.equals("description")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("description");
              System.out.println("Data"+attr);
              sitesList.setdescription(attr);
  
  
          }else if (localName.equals("manufacturer")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("manufacturer");
              System.out.println("Data"+attr);
              sitesList.setmanufacturer(attr);
  
  
          }else if (localName.equals("url")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("url");
              System.out.println("Data"+attr);
              sitesList.seturl(attr);
  
  
          }else if (localName.equals("Image")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("Image");
              System.out.println("Data"+attr);
              sitesList.setImage(attr);
  
  
          }else if (localName.equals("relevancy")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("relevancy");
              System.out.println("Data"+attr);
              sitesList.setrelevancy(attr);
  
  
          }else if (localName.equals("minPrice")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("minPrice");
              System.out.println("Data"+attr);
              sitesList.setminPrice(attr);
  
  
          }else if (localName.equals("maxPrice")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("maxPrice");
              System.out.println("Data"+attr);
              sitesList.setmaxPrice(attr);
  
  
          }else if (localName.equals("stores")) {
              sitesList = new SitesList();
              /** Get attribute value */
              String attr = attributes.getValue("stores");
              System.out.println("Data"+attr);
              sitesList.setstores(attr);
  
  
          }
  
      }
  
      /** Called when tag closing ( ex:- &lt;name&gt;AndroidPeople&lt;/name&gt; 
       * -- &lt;/name&gt; )*/
      @Override
      public void endElement(String uri, String localName, String qName)
              throws SAXException {
  
          currentElement = false;
  
          /** set value */ 
          if (localName.equalsIgnoreCase("title"))
              sitesList.settitle(currentValue);
          else if (localName.equalsIgnoreCase("description"))
              sitesList.setdescription(currentValue);
  
      }
  
      /** Called to get tag characters ( ex:- &lt;name&gt;AndroidPeople&lt;/name&gt; 
       * -- to get AndroidPeople Character ) */
      @Override
      public void characters(char[] ch, int start, int length)
              throws SAXException {
  
          if (currentElement) {
              currentValue = new String(ch, start, length);
              currentElement = false;
          }
  
      }
  
  }
  
  //myparsingexample.java
  package com.androidpeople.xml.parsing;
  
  import java.net.URL;
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  import org.xml.sax.InputSource;
  import org.xml.sax.XMLReader;
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.LinearLayout;
  import android.widget.TextView;
  
  public class XMLParsingExample extends Activity {
  
      /** Create Object For SiteList Class */
      SitesList sitesList = null;
  
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          /** Create a new layout to display the view */
          LinearLayout layout = new LinearLayout(this);
          layout.setOrientation(1);
  
          /** Create a new textview array to display the results */
          TextView Product[];
          TextView categoryId[];
          TextView id[];
          TextView title[];
          TextView description[];
          TextView manufacturer[];
          TextView url[];
          TextView Image[];
          TextView relevancy[];
          TextView minPrice[];
          TextView maxPrice[];
          TextView stores[];
          TextView Products[];
          TextView PriceSet[];
  
  
          try {
  
              /** Handling XML */
              SAXParserFactory spf = SAXParserFactory.newInstance();
              SAXParser sp = spf.newSAXParser();
              XMLReader xr = sp.getXMLReader();
  
              /** Send URL to parse XML Tags */
              URL sourceUrl = new URL(
                      "http://catalog.bizrate.com/services/catalog/v1/us/product?publisherId=50085&amp;placementId=1&amp;categoryId=1&amp;keyword=ipod&amp;productId=&amp;productIdType=SZPID&amp;merchantId=&amp;minPrice=&amp;maxPrice=&amp;start=0&amp;results=10&amp;startOffers=0&amp;resultsOffers=0&amp;sort=relevancy_desc&amp;brandId=&amp;attFilter=&amp;showAttributes=&amp;showProductAttributes=&amp;zipCode=&amp;offersOnly=&amp;biddedOnly=&amp;minRelevancyScore=100&amp;apiKey=58f536aa2fab110bbe0da501150bac1e");
  
              /** Create handler to handle XML Tags ( extends DefaultHandler ) */
              MyXMLHandler myXMLHandler = new MyXMLHandler();
              xr.setContentHandler(myXMLHandler);
              xr.parse(new InputSource(sourceUrl.openStream()));
  
          } catch (Exception e) {
              System.out.println("XML Pasing Excpetion = " + e);
          }
  
          /** Get result from MyXMLHandler SitlesList Object */
          sitesList = MyXMLHandler.sitesList;
  
          /** Assign textview array lenght by arraylist size */
          Product = new TextView[sitesList.getProduct().size()];
          categoryId = new TextView[sitesList.getcategoryId().size()];
          Products = new TextView[sitesList.getProducts().size()];
          PriceSet = new TextView[sitesList.getPriceSet().size()];
          id = new TextView[sitesList.getid().size()];
          title = new TextView[sitesList.gettitle().size()];
          manufacturer = new TextView[sitesList.getmanufacturer().size()];
          description = new TextView[sitesList.getdescription().size()];
          Image = new TextView[sitesList.getImage().size()];
          relevancy = new TextView[sitesList.getrelevancy().size()];
          minPrice = new TextView[sitesList.getminPrice().size()];
          maxPrice = new TextView[sitesList.getmaxPrice().size()];
          stores = new TextView[sitesList.getstores().size()];
          url = new TextView[sitesList.geturl().size()];
  
          /** Set the result text in textview and add it to layout */
          for (int i = 0; i &lt; sitesList.getProduct().size(); i++) {
              Product[i] = new TextView(this);
              Product[i].setText("Products = "+sitesList.getProducts().get(i));
              Products[i] = new TextView(this);
              Products[i].setText("Products = "+sitesList.getProducts().get(i));
              PriceSet[i] = new TextView(this);
              PriceSet[i].setText("PriceSet = "+sitesList.getPriceSet().get(i));
              categoryId[i] = new TextView(this);
              categoryId[i].setText("categoryId = "+sitesList.getcategoryId().get(i));
              id[i] = new TextView(this);
              id[i].setText(" id = "+sitesList.getid().get(i));
              title[i] = new TextView(this);
              title[i].setText("title = "+sitesList.gettitle().get(i));
              description[i] = new TextView(this);
              description[i].setText("description = "+sitesList.getdescription().get(i));
              manufacturer[i] = new TextView(this);
              manufacturer[i].setText(" manufacturer = "+sitesList.getmanufacturer().get(i));
              url[i] = new TextView(this);
              url[i].setText("url = "+sitesList.geturl().get(i));
              Image[i] = new TextView(this);
              Image[i].setText("Image = "+sitesList.getImage().get(i));
              relevancy[i] = new TextView(this);
              relevancy[i].setText(" relevancy = "+sitesList.getrelevancy().get(i));
              minPrice[i] = new TextView(this);
              minPrice[i].setText("minPrice = "+sitesList.getminPrice().get(i));
              maxPrice[i] = new TextView(this);
              maxPrice[i].setText("maxPrice = "+sitesList.getmaxPrice().get(i));
              stores[i] = new TextView(this);
              stores[i].setText(" id = "+sitesList.getstores().get(i));
  
              layout.addView(Product[i]);
              layout.addView(categoryId[i]);
              layout.addView(id[i]);
              layout.addView(title[i]);
              layout.addView(description[i]);
              layout.addView(manufacturer[i]);
              layout.addView(url[i]);
              layout.addView(Image[i]);
              layout.addView(relevancy[i]);
              layout.addView(minPrice[i]);
              layout.addView(maxPrice[i]);
              layout.addView(stores[i]);
              layout.addView(description[i]);
          }
  
          /** Set the layout view to display */
          setContentView(layout);
  
      }
  }
  </code></pre>
  
  <p>The value is not coming in ddms also?</p>