title: Parsing XML and getting a URL value to download a file
body: <p>I am using a tutorial about a listview <a href="http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/" rel="nofollow">here</a> which get some information like the names of the artist... and the URL for the image of artist and making a listview. </p>
  
  <pre><code>    &lt;music&gt;
      &lt;song&gt;
      &lt;id&gt;1&lt;/id&gt;
      &lt;title&gt;Someone Like You&lt;/title&gt;
      &lt;artist&gt;Adele&lt;/artist&gt;
      &lt;duration&gt;4:47&lt;/duration&gt;
      &lt;thumb_url&gt;http://api.androidhive.info/music/images/adele.png&lt;/thumb_url&gt;
      &lt;/song&gt;
      &lt;song&gt;
      &lt;id&gt;2&lt;/id&gt;
      &lt;title&gt;Space Bound&lt;/title&gt;
      &lt;artist&gt;Eminem&lt;/artist&gt;
      &lt;duration&gt;4:38&lt;/duration&gt;
      &lt;thumb_url&gt;
      http://api.androidhive.info/music/images/eminem.png
      &lt;/thumb_url&gt;
      &lt;/song&gt;
      .
      .
      .
  </code></pre>
  
  <p>I want to add another node called mp3 and give the song's URL to each array in the XML so that by clicking each row of the list the proper mp3 file be downloaded. Like below:</p>
  
  <pre><code>&lt;music&gt;
  &lt;song&gt;
  &lt;id&gt;1&lt;/id&gt;
  &lt;title&gt;Someone Like You&lt;/title&gt;
  &lt;artist&gt;Adele&lt;/artist&gt;
  &lt;duration&gt;4:47&lt;/duration&gt;
  &lt;thumb_url&gt;http://api.androidhive.info/music/images/adele.png&lt;/thumb_url&gt;
  **&lt;mp3&gt; some URL1 &lt;/mp3&gt;**
  &lt;/song&gt;
  &lt;song&gt;
  &lt;id&gt;2&lt;/id&gt;
  &lt;title&gt;Space Bound&lt;/title&gt;
  &lt;artist&gt;Eminem&lt;/artist&gt;
  &lt;duration&gt;4:38&lt;/duration&gt;
  &lt;thumb_url&gt;http://api.androidhive.info/music/images/eminem.png&lt;/thumb_url&gt;
  **&lt;mp3&gt; some URL2 &lt;/mp3&gt;**
  &lt;/song&gt;
  .
  .
  .
  </code></pre>
  
  <p>I changed the main java code like below but I can just download the "file_url" (new DownloadFileFromURL().execute(<strong>file_url</strong>);) string which is defined as direct URL entered before. Please help me to get the mp3 URL from xml for download.</p>
  
  <pre><code>    public class CustomizedListView extends Activity {
  // All static variables
  static final String URL = "http://api.androidhive.info/music/music.xml";
  // XML node keys
  static final String KEY_SONG = "song"; // parent node
  static final String KEY_ID = "id";
  static final String KEY_TITLE = "title";
  static final String KEY_ARTIST = "artist";
  static final String KEY_DURATION = "duration";
  static final String KEY_THUMB_URL = "thumb_url";
  
  ListView list;
  LazyAdapter adapter;
  
    // Progress Dialog
  private ProgressDialog pDialog;
    // Progress dialog type (0 - for Horizontal progress bar)
  public static final int progress_bar_type = 0;
    // File url to download
  private static String file_url = "the URL of the file that I don't want to use this method for downloading the file";
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
  
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
      XMLParser parser = new XMLParser();
      String xml = parser.getXmlFromUrl(URL); // getting XML from URL
      Document doc = parser.getDomElement(xml); // getting DOM element
  
      NodeList nl = doc.getElementsByTagName(KEY_SONG);
      // looping through all song nodes &lt;song&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_TITLE, parser.getValue(e, KEY_TITLE));
          map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
          map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
          map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
  
          // adding HashList to ArrayList
          songsList.add(map);
      }
  
  
      list=(ListView)findViewById(R.id.list);
  
      // Getting adapter by passing xml data ArrayList
      adapter=new LazyAdapter(this, songsList);        
      list.setAdapter(adapter);
  
  
  
  
  
      // Click event for single list row
      list.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                  int position, long id) {
              new DownloadFileFromURL().execute(file_url);
  
  
  
          }
      }); 
  
  }   
  /**
   * Showing Dialog
   * */
  @Override
  protected Dialog onCreateDialog(int id) {
      switch (id) {
      case progress_bar_type:
          pDialog = new ProgressDialog(this);
          pDialog.setMessage("Downloading file. Please wait...");
          pDialog.setIndeterminate(false);
          pDialog.setMax(100);
          pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
          pDialog.setCancelable(true);
          pDialog.show();
          return pDialog;
      default:
          return null;
      }
  }
  
  /**
   * Background Async Task to download file
   * */
  class DownloadFileFromURL extends AsyncTask&lt;String, String, String&gt; {
  
      /**
       * Before starting background thread
       * Show Progress Bar Dialog
       * */
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          showDialog(progress_bar_type);
      }
  
      /**
       * Downloading file in background thread
       * */
      @Override
      protected String doInBackground(String... f_url) {
          int count;
          try {
              URL url = new URL(f_url[0]);
              URLConnection conection = url.openConnection();
              conection.connect();
              // getting file length
              int lenghtOfFile = conection.getContentLength();
  
              // input stream to read file - with 8k buffer
              InputStream input = new BufferedInputStream(url.openStream(), 8192);
  
              // Output stream to write file
              OutputStream output = new  FileOutputStream("/sdcard/Downloadmanager.zip");
  
              byte data[] = new byte[1024];
  
              long total = 0;
  
              while ((count = input.read(data)) != -1) {
                  total += count;
                  // publishing the progress....
                  // After this onProgressUpdate will be called
                  publishProgress(""+(int)((total*100)/lenghtOfFile));
  
                  // writing data to file
                  output.write(data, 0, count);
              }
  
              // flushing output
              output.flush();
  
              // closing streams
              output.close();
              input.close();
  
          } catch (Exception e) {
              Log.e("Error: ", e.getMessage());
          }
  
          return null;
      }
  
      /**
       * Updating progress bar
       * */
      protected void onProgressUpdate(String... progress) {
          // setting progress percentage
          pDialog.setProgress(Integer.parseInt(progress[0]));
     }
  
      /**
       * After completing background task
       * Dismiss the progress dialog
       * **/
      @Override
      protected void onPostExecute(String file_url) {
          // dismiss the dialog after the file was downloaded
          dismissDialog(progress_bar_type);
  
  
      }
  
  }
  </code></pre>
  
  <p>}</p>