0

我想从 xml 访问特定属性,就像在这个例子中一样,有 2 个图像标签,但 2 个不同的属性,大小为小,大小为中,所以我如何访问中

<image size="small">http://userserve-ak.last.fm/serve/34/62210477.png</image><image size="medium">http://userserve-ak.last.fm/serve/64/62210477.png</image>

我试过这个它适用于较低的android版本,但它不适用于4.0

XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        try {
       expr = xpath.compile("//image[@size=\"large\"]");
      nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);

这是完整的代码

public class loadSomeStuff extends AsyncTask<Void, Void, String>
{
    XPathExpression expr;
    NodeList nl;
    int i;
    String name="test";

    @Override
    protected String doInBackground(Void... params) {
        // TODO Auto-generated method stub

        final String KEY_NAME = "name";
        final String KEY_IMAGE ="image";
        //final String KEY_COST = "cost";
        //final String KEY_DESC = "description";
        String URL = "http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=enrique_iglesias&api_key=b25b959554ed76058ac220b7b2e0a026&limit=" + 1 + "&page=" + 1;
        XmlParser parser = new XmlParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element
        //XPathFactory xPathfactory = XPathFactory.newInstance();
        //XPath xpath = xPathfactory.newXPath();
        //try {
        //  expr = xpath.compile("//image[@size=\"large\"]");
            //nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
                    NodeList nl = doc.getElementsByTagName("artist");
             for (i = 0; i < nl.getLength(); i++) 
             {
                    Element e = (Element) nl.item(i);
                     name = parser.getValue(e, KEY_NAME);// name child value
                        image = parser.getValue(e, KEY_IMAGE);
                        System.out.print(image);
                        Log.v(image, "image url");
                        return image;
        } 


        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

            URL thumb_u;
            try {
                thumb_u = new URL(result);
                  Drawable thumb_d = Drawable.createFromStream(thumb_u.openStream(), "src");
                  Toast toast = Toast.makeText(myActionbar.this, image, Toast.LENGTH_LONG);
                  toast.show();
                    icon.setImageDrawable(thumb_d);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


    }

here is My Xmlparserfile in which my getvalue  and get elements are defined

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 "";
  } 

}

4

1 回答 1

0

尝试这个:

    XMLParser parser = new XMLParser();
    String URL = "http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=akon&api_key=your_api_key";
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName("album");

    for (int i = 0; i < nl.getLength(); i++) {
        Element e = (Element) nl.item(i);
        Log.e("name", parser.getValue(e, "name"));
        NodeList k = e.getElementsByTagName("image");
        for (int j = 0; j < k.getLength(); j++) {
            Element e1 = (Element) k.item(j);
            if(e1.getAttribute("size").equals("large"))
            Log.e("ImageURL", parser.getValue(e1, "image"));
        }
    }
于 2013-04-10T15:24:49.643 回答