2010-07-12 50 views
1

我想做一個應用程序,將使用來自xml數據庫的數據,有兩點,我需要幫助,因爲我發現Java與XML太複雜。得到一個單一的XML節點 - android dev

例如

<items> 
<1><name>box</name><price>2.00</price></1> 
<2><name>pencil</name><price>1.00</price></1> 
</items> 
  1. 得到一個子節點(第1項的eg.name - 「盒子」)一個XML值
  2. 得到子節點的XML數據庫(使用它作爲數據源的一個列表框) - 這一個是沒有必要的。

回答

0

您的問題在於,XML節點必須以字母開頭 - 就像Java中的變量一樣。你可以試試下面的:

<items> 
    <item name="box" price="2.00" /> 
    <item name="pencil" price="1.00" /> 
</items> 

所以你看,每個item被表示爲<item>。在XML中,標籤具有語義意義。您可以將它們這樣做(取決於你的分析器 - 這是實際上更接近JavaScript,但我希望你能明白我的意思):

myXmlDocument = parseThatXMLForMePlease(); // Load the XMl from wherever 
itemList = myXmlDocument.getChildren()[0]; // Get the first node in the document 
firstItem = itemList.getChildren()[0];  // Get the first child of the item list 
itemName = firstItem.getAttr("name"); 
itemPrice = firstItem.getAttr("price"); 

如果你想利用這些數據爲基礎,一些列表框,你可能會想要使用Adapter。這基本上是一個數據結構,用於爲一些UI小部件提供數據(鏈接指向一個ListAdapter - 這聽起來像是你想要的)。