如何将一个子项添加到包含多个项的列表中? [英] How to append a child to a list containing more than one item.?

查看:149
本文介绍了如何将一个子项添加到包含多个项的列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的xml看起来像这样:

My xml looks like this :

<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年添加新条目时,我检查该年份是否已经存在;如果要添加,我想进入年份并查看Month是否存在,如果也存在,那么我检查该年份是否存在。当前日期已经存在。如果是这样,我将在日期列表中添加新条目,如果没有,则在日期列表中创建一个新节点。几年和几个月都一样。我很难解决这个问题。任何帮助是极大的赞赏。

Here is what I am trying to do: I am developing an android application that logs and keeps track of daily expenses. So, when a user adds a new entry for the year 2015, I check if that year already exists, If it does I want to go inside the years and see if the Month exists, and if it does too, then I check if the current day already exists. If it does, I add the new entry in the Day list and if it doesn't I create a new node in the Day list. Same goes for years and months. I am having a hard time figuring this out. Any help is greatly appreciated.

推荐答案

appendChild() XML 类的方法,并用于 XMLList 视为 XML 对象它应该具有一个XML元素


对于只包含一个XML元素的XMLList对象,可以使用所有属性以及XML类的方法,因为带有一个XML元素的XMLList被视为与XML对象相同...

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 返回两个元素,并且这就是为什么您会收到该错误。

In your case, xml.Year return two elements and that's why you got that error.

因此,要避免这种情况,您必须获得带有元素的 XMLList 对象,例如,仅选择名称为 2016的年份:

So to avoid that, you have to get an XMLList object with one element like, for example, selecting only the "Year" where the name is "2016" :

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

希望有帮助。

这篇关于如何将一个子项添加到包含多个项的列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆