开发者

parser 2.1 and 2.2

i using the follwing Code to retrive XML element text using getElementsByTagName

this code success in 2.2 and Failed in 2.1

any idea ?

URL metafeedUrl = new URL("http://x..../Y.xml")
URLConnection connection ;     
connection= metafeedUrl.openConnection();

HttpURLConnection  httpConnection = (HttpURLConnection)connection ;        
int resposnseCode= httpConnection.getResponseCode() ;

if (resposnseCode == HttpURLConnection.HTTP_OK) {

InputStream in = httpConnection.getInputStream();

DocumentBuilderFactory dbf ;
dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();


// Parse the Earthquakes entry 
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();

//ArrayList<Album> Albums = new ArrayList<Album>();


/*开发者_运维问答 Returns a NodeList of all descendant Elements with a given tag name, in document order.*/

NodeList nl = docEle.getElementsByTagName("entry");

if (nl!=null && nl.getLength() > 0) {        
   for (int i = 0; i < nl.getLength(); i++) {

Element entry = (Element)nl.item(i);

/* Now on every property in Entry **/

Element title =(Element)entry.getElementsByTagName("title").item(0);          

*Here i Get an Error*
String album_Title = title.getTextContent();

Element id =(Element)entry.getElementsByTagName("id").item(0);         
String album_id = id.getTextContent(); // 


getTextContent() is not supported in API 7 (Android 2.1). It was introduced in API 8 (2.2).

Assuming a predictable result from the server, you can use the following code:

Node n = aNodeList.item(i);

String strValue = n.getFirstChild().getNodeValue();

// as opposed to the String strValue = n.getTextContent();

If the element may be empty, then you'd want to check the child count first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜