如何将 child 附加到包含多个项目的列表中。?

How to append a child to a list containing more than one item.?

我的 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 年的新条目时,我会检查该年份是否已经存在,如果已经存在,我想进入这些年份并查看月份是否存在,如果存在,那么我会检查是否存在当前日期已经存在。如果是,我将新条目添加到 Day 列表中,如果不是,我将在 Day 列表中创建一个新节点。数年和数月也是如此。我很难弄清楚这一点。任何帮助是极大的赞赏。

appendChild() is an XML class's method, and for an XMLList to be treated as an XML object 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 return 两个元素,这就是您出现该错误的原因。

因此,为避免这种情况,您必须获得一个包含一个元素的 XMLList 对象,例如,仅选择名称为“2016”的 "Year":

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

希望能帮到你。