title: Android Xml data parsing
body: <p>I am trying to parse XML and display it into list view but after running the app nothing happen the list display but not with XML data. I don't know if I missing something please can help me.</p>
  
  <p><strong>MainActivity class</strong></p>
  
  <pre><code>    public class MainActivity extends ListActivity {
      // All static variables
      static final String URL = "http://api.androidhive.info/pizza/?format=xml";
      // XML node keys
      static final String KEY_ITEM = "item"; // parent node
      static final String KEY_ID = "id";
      static final String KEY_NAME = "name";
      static final String KEY_COST = "cost";
      static final String KEY_DESC = "description";
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if (android.os.Build.VERSION.SDK_INT &gt; 9) {
              StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
              StrictMode.setThreadPolicy(policy);
          }
          ListView myList=(ListView)findViewById(android.R.id.list);
  
          ArrayList&lt;HashMap&lt;String, String&gt;&gt; menuItems = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
          XMLParser parser = new XMLParser();
          String xml = null; // getting XML
          try {
              xml = parser.getXmlFormUrl(URL);
          } catch (IOException e) {
              e.printStackTrace();
          }
          Document doc = parser.getDomElement(xml); // getting DOM element
          NodeList nl = doc.getElementsByTagName(KEY_ITEM);
          // looping through all item nodes &lt;item&gt;
          for (int i = 0; i &lt; nl.getLength(); i++) {
              // creating new HashMap
              HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
              Element e = (Element) nl.item(i);
              // adding each child node to HashMap key =&gt; value
              map.put(KEY_ID, parser.getValue(e, KEY_ID));
              map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
              map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
              map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
  
              // adding HashList to ArrayList
              menuItems.add(map);
          }
          // Adding menuItems to ListView
          ListAdapter adapter = new SimpleAdapter(this, menuItems,
                  R.layout.activity_main,
                  new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                  R.id.name, R.id.desciption, R.id.cost });
  
          setListAdapter(adapter);
          // selecting single ListView item
  
      }
  
  
  }
  </code></pre>
  
  <p><strong>XmlParser class</strong></p>
  
  <pre><code>    public class XMLParser {
          String result;
          public String getXmlFormUrl(String link) throws IOException{
          URL url=new URL(link.toString());
          HttpURLConnection UrlConnection= (HttpURLConnection)     url.openConnection();
          int status=UrlConnection.getResponseCode();
          if(status==200){
              InputStream inputStream=UrlConnection.getInputStream();
              BufferedReader bufferedReader=new BufferedReader(new     InputStreamReader(inputStream,"UTF8"));
              StringBuilder stringBuilder= new StringBuilder();
              String line;
              while ((line=bufferedReader.readLine())!=null){
                  stringBuilder.append((line+"\n"));
              }
              result=stringBuilder.toString();
              inputStream.close();
          }
          return  result;
      }
      public Document getDomElement(String xml){
          Document doc = null;
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          try {
  
              DocumentBuilder db = dbf.newDocumentBuilder();
  
              InputSource is = new InputSource();
              is.setCharacterStream(new StringReader(xml));
              doc = db.parse(is);
  
          } catch (ParserConfigurationException e) {
              Log.e("Error: ", e.getMessage());
              return null;
          } catch (SAXException e) {
              Log.e("Error: ", e.getMessage());
              return null;
          } catch (IOException e) {
              Log.e("Error: ", e.getMessage());
              return null;
          }
          // return DOM
          return doc;
      }
      public String getValue(Element item, String str) {
          NodeList n = item.getElementsByTagName(str);
          return this.getElementValue(n.item(0));
      }
  
      public final String getElementValue( Node elem ) {
          Node child;
          if( elem != null){
              if (elem.hasChildNodes()){
                  for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                      if( child.getNodeType() == Node.TEXT_NODE  ){
                          return child.getNodeValue();
                      }
                  }
              }
          }
          return "";
      }
  }
  </code></pre>
  
  <p><strong>ActivityMain.xml</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      tools:context="android.prgguru.com.xmlparsingg.MainActivity"&gt;
  
      &lt;ListView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@android:id/list"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="73dp" /&gt;
  &lt;/RelativeLayout&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>after this line I think the list should  display with XML data</strong> but nothing happens  </p>
  
  <pre><code> myList.setAdapter(adapter);
  </code></pre>
  
  <p><strong>ids.xml</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;resources&gt;
      &lt;item name="desciption" type="id" /&gt;
      &lt;item name="cost" type="id" /&gt;
      &lt;item name="name" type="id" /&gt;
      &lt;item name="List" type="id"/&gt;
  &lt;/resources&gt;</code></pre>
  </div>
  </div>
  </p>