views:

1155

answers:

1

i have an xml document:

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>);

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>");

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 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!

+3  A: 

There are two problems here (I've now tested this code - it should work for you):

  1. 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.

  2. 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.

Try this instead:

var contentNode:XML = xml.mainSection.itemList.item[0];
var parentNode:XML = xml.mainSection.itemList[0];
parentNode.insertChildBefore( contentNode, newNode[0] );
Matt Dillard
thanks! I think we're on the right track here but parentNode is coming up as null.
nerdabilly
Alright, I've updated the answer with something that works for me.
Matt Dillard
that worked! thanks!
nerdabilly