Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid DOM parser, get first item in list
    text
    copied!<p>I am simply trying to get only the first <code>&lt;song&gt;</code> in my list; the code I am using works fine, but only gets the LAST entry of similar tags (<code>&lt;song&gt;</code>) in my list. Here's my XML:</p> <pre><code> &lt;songs id="stationid"&gt; &lt;song&gt; &lt;title&gt; &lt;![CDATA[ Last Breath ]]&gt; &lt;/title&gt; &lt;artist&gt; &lt;![CDATA[ Xela ]]&gt; &lt;/artist&gt; &lt;album&gt; &lt;![CDATA[ For Frosty Mornings And Summer Nights ]]&gt; &lt;/album&gt; &lt;albumart&gt; &lt;![CDATA[ ]]&gt; &lt;/albumart&gt; &lt;date&gt;1344536317&lt;/date&gt; &lt;/song&gt; &lt;song&gt; &lt;title&gt; &lt;![CDATA[ Turn On Switch Off (Starring Valerie Trebeljahr) ]]&gt; &lt;/title&gt; &lt;artist&gt; &lt;![CDATA[ Static ]]&gt; &lt;/artist&gt; &lt;album&gt; &lt;![CDATA[ Flavour Has No Name ]]&gt; &lt;/album&gt; &lt;albumart&gt; &lt;![CDATA[ ]]&gt; &lt;/albumart&gt; &lt;date&gt;1344536000&lt;/date&gt; &lt;/song&gt; &lt;song&gt; &lt;title&gt; &lt;![CDATA[ Aim Low ]]&gt; &lt;/title&gt; &lt;artist&gt; &lt;![CDATA[ Sheik &amp; Beige ]]&gt; &lt;/artist&gt; &lt;album&gt; &lt;![CDATA[ Sty Wars - A Collection of Por ]]&gt; &lt;/album&gt; &lt;albumart&gt; &lt;![CDATA[ ]]&gt; &lt;/albumart&gt; &lt;date&gt;1344531485&lt;/date&gt; &lt;/song&gt; &lt;/songs&gt; </code></pre> <p>and my code: (I want to retrieve the first list item only, as the feed is updated as the song changes and I want the currently playing song which is always at the top of the list)</p> <pre><code> package com.example.networking; import android.app.Activity; import android.os.Bundle; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import android.util.Log; import android.widget.ImageView; import android.widget.Toast; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import java.io.InputStreamReader; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class NetworkingActivity extends Activity { private InputStream OpenHttpConnection(String urlString) throws IOException { InputStream in = null; int response = -1; URL url = new URL(urlString); URLConnection conn = url.openConnection(); if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection"); try{ HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } } catch (Exception ex) { Log.d("Networking", ex.getLocalizedMessage()); throw new IOException("Error connecting"); } return in; } private String WordDefinition(String word) { InputStream in = null; String strDefinition = ""; try { in = OpenHttpConnection( "http://api.somafm.com/songs/groovesalad.xml"); Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db; try { db = dbf.newDocumentBuilder(); doc = db.parse(in); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } doc.getDocumentElement().normalize(); //---retrieve all the &lt;song&gt; elements--- NodeList definitionElements = doc.getElementsByTagName("song"); //---iterate through each &lt;song&gt; elements--- for (int i = 0; i &lt; definitionElements.getLength(); i++) { Node itemNode = definitionElements.item(i); if (itemNode.getNodeType() == Node.ELEMENT_NODE) { //---convert the song node into an Element--- Element definitionElement = (Element) itemNode; //---get all the &lt;artist&gt; elements under // the &lt;song&gt; element--- NodeList wordDefinitionElements = (definitionElement).getElementsByTagName( "artist"); strDefinition = ""; //---iterate through each &lt;artist&gt; elements--- for (int j = 0; j &lt; wordDefinitionElements.getLength(); j++) { //---convert an &lt;artist&gt; node into an Element--- Element wordDefinitionElement = (Element) wordDefinitionElements.item(j); //---get all the child nodes under the // &lt;artist&gt; element--- NodeList textNodes = ((Node) wordDefinitionElement).getChildNodes(); strDefinition += ((Node) textNodes.item(0)).getNodeValue() + ". \n"; } } } } catch (IOException e1) { Log.d("NetworkingActivity", e1.getLocalizedMessage()); } //---return the definitions of the word--- return strDefinition; } private class AccessWebServiceTask extends AsyncTask&lt;String, Void, String&gt; { protected String doInBackground(String... urls) { return WordDefinition(urls[0]); } protected void onPostExecute(String result) { Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show(); } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //---access a Web Service using GET--- new AccessWebServiceTask().execute("apple"); } } </code></pre> <p>Thank you if anyone can help me!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload