Android DOM解析器,获取列表中的第一项

时间:2012-08-09 18:37:11

标签: android dom

我只是想在我的列表中只获取第一个<song>;我正在使用的代码工作正常,但只在我的列表中获取类似标记的最后条目(<song>)。这是我的XML:

    <songs id="stationid">
<song>
<title>
<![CDATA[ Last Breath ]]>
</title>
<artist>
<![CDATA[ Xela ]]>
</artist>
<album>
<![CDATA[ For Frosty Mornings And Summer Nights ]]>
</album>
<albumart>
<![CDATA[ ]]>
</albumart>
<date>1344536317</date>
</song>
<song>
<title>
<![CDATA[ Turn On Switch Off (Starring Valerie Trebeljahr) ]]>
</title>
<artist>
<![CDATA[ Static ]]>
</artist>
<album>
<![CDATA[ Flavour Has No Name ]]>
</album>
<albumart>
<![CDATA[ ]]>
</albumart>
<date>1344536000</date>
</song>
<song>
<title>
<![CDATA[ Aim Low ]]>
</title>
<artist>
<![CDATA[ Sheik & Beige ]]>
</artist>
<album>
<![CDATA[ Sty Wars - A Collection of Por ]]>
</album>
<albumart>
<![CDATA[ ]]>
</albumart>
<date>1344531485</date>
</song>
    </songs>

和我的代码:(我想只检索第一个列表项,因为当歌曲发生变化时,Feed会更新,我希望当前播放的歌曲始终位于列表的顶部)

    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 <song> elements---
        NodeList definitionElements = 
            doc.getElementsByTagName("song"); 

        //---iterate through each <song> elements---
        for (int i = 0; i < 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 <artist> elements under 
                // the <song> element---
                NodeList wordDefinitionElements = 
                    (definitionElement).getElementsByTagName(
                    "artist");

                strDefinition = "";
                //---iterate through each <artist> elements---
                for (int j = 0; j < wordDefinitionElements.getLength(); j++) {                    
                    //---convert an <artist> node into an Element---
                    Element wordDefinitionElement = 
                        (Element) wordDefinitionElements.item(j);

                    //---get all the child nodes under the 
                    // <artist> 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<String, Void, String> {
    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");

}
}

感谢有人可以帮助我!

1 个答案:

答案 0 :(得分:0)

当你第一次点击内部循环中的最后一个语句时,你应该在WordDefinition中打破外部for循环。您可以这样做,例如,通过设置标志并在for

中对其进行测试
boolean flag = false;    
for (int i = 0; i < definitionElements.getLength && !flag; i++) {
...
    strDefinition += 
                    ((Node) textNodes.item(0)).getNodeValue() + ". \n";
    flag = true;
    break;

我更喜欢使用XPath,如下所示:

private String WordDefinition(String word) {
    InputStream in = null;
    String strDefinition = "";
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath = xPathFactory.newXPath();

    try {
        in = OpenHttpConnection(
                "http://api.somafm.com/songs/groovesalad.xml");
        XPathExpression xPathExpression = xPath.compile("/songs/song[1]/artist");
        Node node = (Node) xPathExpression.evaluate(new InputSource(in), XPathConstants.NODE);
        strDefinition = node.getTextContent();
    }
    catch (IOException e1) {
        Log.d("NetworkingActivity", e1.getLocalizedMessage());
    }
    catch (XPathExpressionException e1) {
        Log.d("NetworkingActivity", e1.getLocalizedMessage());
    }
    // ---return the definitions of the word---
    return strDefinition;
}