我有一个 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 的开头插入另一个。
var newNode:XMLList = new XMLList("<item><video src=\"" + _videoSource + "\"></video></item>");
我能够很好地生成和跟踪 newNode ,但是每当我尝试使用添加它时insertChildBefore
,它总是返回未定义。
var contentNode:XML = new XML(xml.mainSection.itemList.item);
xml.insertChildBefore(contentNode ,newNode)
contentNode
总是追踪得很好,但尝试时总是失败insertChildBefore
or insertChildAfter
。奇怪的是,如果我做contentNode
不太具体(比如xml.mainSection
)然后它按预期工作。
感谢您的帮助,这让我发疯!
这里有两个问题(我现在已经测试了这段代码 - 它应该适合你):
变量xml
不是该对象的直接父对象item
您正在插入的节点。你正在呼唤insertChildBefore
on the xml
节点,但是你的contentNode
不是它的直接子代。
The contentNode
您尝试在前面插入的变量是copy您想要的节点;您不应该创建一个全新的 XML 对象。
试试这个:
var contentNode:XML = xml.mainSection.itemList.item[0];
var parentNode:XML = xml.mainSection.itemList[0];
parentNode.insertChildBefore( contentNode, newNode[0] );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)