2012-08-01 88 views
1

我想列出所有在Android 4.0中的標記的值截至目前我能夠得到只有一個值我已經添加了我現在使用的片段&也xml below.Please幫助這個與片段或example.Thanks很多。如何從android 4.0中的子節點獲取所有值?

NodeList nodes = doc.getElementsByTagName("month"); 

對(INT I = 0;我< nodes.getLength();我++){

Element e = (Element) nodes.item(i); 

      stock_list.add(getValue(e, "month")); 
} 



Here is my xml 

enter image description here

+0

在循環內部添加'Log.i(e.toString())'。它輸出什麼?你的方法'getValue'(你沒有向我們展示過)做什麼? – Heinzi 2012-08-01 05:20:37

+0

有沒有其他方法可以獲得標記的所有值。 – Karthik 2012-08-01 05:29:02

+0

'e.getTextContent()'? – Heinzi 2012-08-01 05:41:55

回答

1

nodeValue的XML元素根據定義是null。因此,您可以

  • 查詢元素的包含實際內容的Text節點的孩子,或
  • 只需使用e.getTextContent()檢索XML元素的文本內容。
0

嘗試此代碼我有同樣的問題,解決了它這種方式..

ArrayList<HashMap<String, String>> stock_list = new ArrayList<HashMap<String, String>>(); 

NodeList nodes = doc.getElementsByTagName("month"); 

for (int i = 0; i < nodes.getLength(); i++) { 
    HashMap<String, String> map = new HashMap<String, String>(); 
    Node name = nodes.item(i); 
    NodeList month = name.getChildNodes(); 
    map.put("months", ((Node) month.item(0)).getNodeValue()); 
    stock_list.add(map); 
}