2015-10-20 69 views
0

我的XML看起來是這樣的:如何將一個孩子追加到包含多個項目的列表中。

<Database> 
<Year name="2015" spent="" saved=""> 
<Month name="" spent="" saved=""> 
    <Day date="" name="" spent="" saved=""> 
    <Entry title="" category="" currency="" cost="" time=""/> 
    </Day> 
</Month> 
</Year> 

<Year name="2016" spent="" saved="150"> 
<Month name="" spent="" saved=""> 
    <Day date="" name="" spent="" saved=""> 
    <Entry title="" category="" currency="" cost="" time=""/> 
    </Day> 
</Month> 

當我使用

myXML.Year.appendChild(<Month name="2016" spent="150" saved="152"></Month>); 

我得到以下錯誤:

TypeError: Error #1086: The appendChild method only works on lists  containing one item. 
    at XMLList/http://adobe.com/AS3/2006/builtin::appendChild() 

任何想法?

這是我正在做的事情:我正在開發一個android應用程序,記錄並跟蹤日常開支。因此,當用戶爲2015年添加新條目時,我會檢查該年是否已經存在,如果我想在年內進入並查看月份是否存在,以及是否存在,那麼我檢查是否存在當前日子已經存在。如果是這樣,我在日期列表中添加新條目,如果沒有,我會在日期列表中創建一個新節點。幾年和幾個月也是如此。我很難弄清楚這一點。任何幫助是極大的讚賞。

回答

1

appendChild()XML類的方法,併爲XMLList被視爲XML對象it should have one XML element

For an XMLList object that contains exactly one XML element, you can use all properties and methods of the XML class, because an XMLList with one XML element is treated the same as an XML object ...

在你的情況,xml.Year回報兩個因素,這就是爲什麼你得到了這個錯誤。

因此,爲了避免這種情況,你必須得到一個XMLList對象與一個元素等,例如,只選擇「年」裏的名字是「2016」:

xml.Year.(@name == '2016').appendChild(<Month name="2016" spent="150" saved="152"></Month>) 

希望能有所幫助。

+0

太棒了,非常感謝! –

相關問題