我有一个XML文档:
var xml:XML = new XML(<rootNode>
<head>
<meta name="template" content="Default" />
</head>
<mainSection>
<itemList>
<item>
<video src={this.videoURL} />
<img src={this.src}></img>
</item>
</itemList>
</mainSection>
</rootNode>);
我想要做的,就是在一定的条件下是我在ITEMLIST的开头插入另一个。
What I'd like to do, is when certain conditions are me, insert another at the beginning of itemList.
var newNode:XMLList = new XMLList("<item><video src=\"" + _videoSource + "\"></video></item>");
我能够生成和跟踪newNode就好了,但每当我尝试使用添加它 insertChildBefore
,它总是返回undefined。
I'm able to generate and trace newNode just fine, but whenever I try to add it using insertChildBefore
, it always returns undefined.
var contentNode:XML = new XML(xml.mainSection.itemList.item);
xml.insertChildBefore(contentNode ,newNode)
contentNode
总是痕迹很好,但它一直在努力失败时 insertChildBefore
或 insertChildAfter
。奇怪的是,如果我做 contentNode
不太具体(如 xml.mainSection
),那么它将按预期工作。
contentNode
always traces fine, but it always fails when trying insertChildBefore
or insertChildAfter
. The weird thing is, if I make contentNode
less specific (like xml.mainSection
) then it works as expected.
感谢您的帮助,这是推动我疯了!
Thanks for any help, this is driving me insane!
有两个问题在这里(我现在已经测试了这个code - 它应该为你工作):
There are two problems here (I've now tested this code - it should work for you):
变量 XML
不是项目
节点要插入的直接父。你打电话 insertChildBefore
的 XML
节点上,但你的 contentNode
不是直接子。
The variable xml
is not the direct parent of the item
node you are inserting. You're calling insertChildBefore
on the xml
node, but your contentNode
is not its direct child.
在 contentNode
变量你想插入提前一个的复制的你想要的节点;你不应该创建一个全新的XML对象。
The contentNode
variable you're trying to insert ahead of is a copy of the node you want; you shouldn't be creating a brand new XML object.
试试这个来代替:
var contentNode:XML = xml.mainSection.itemList.item[0];
var parentNode:XML = xml.mainSection.itemList[0];
parentNode.insertChildBefore( contentNode, newNode[0] );