2008-10-09 38 views
3

與insertChildBefore和insertChildAfter麻煩我有一個XML文檔:有在AS3

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添加它,它總是返回undefined。

var contentNode:XML = new XML(xml.mainSection.itemList.item); 
xml.insertChildBefore(contentNode ,newNode) 

contentNode總是跟蹤正常,但試圖insertChildBeforeinsertChildAfter時總是失敗。奇怪的是,如果我使contentNode不那麼具體(如xml.mainSection),那麼它按預期工作。

感謝您的幫助,這讓我瘋狂!

回答

5

這裏有兩個問題(我現在已經測試此代碼 - 它應該爲你工作):

  1. 變量xml是不是你要插入的item節點的直接父。您在xml節點上致電insertChildBefore,但您的contentNode不是其直接子節點。

  2. 您嘗試插入的contentNode變量是您想要的節點的副本;你不應該創建一個全新的XML對象。

試試這個:

var contentNode:XML = xml.mainSection.itemList.item[0]; 
var parentNode:XML = xml.mainSection.itemList[0]; 
parentNode.insertChildBefore(contentNode, newNode[0]); 
+0

的感謝!我認爲我們在這裏正確的軌道,但parentNode是空的。 – nerdabilly 2008-10-09 20:38:04