Problems with insertChildBefore and insertChildAfter in AS3

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 would like to do is when I need certain conditions, insert another at the beginning of the itemList.

var newNode:XMLList = new XMLList("<item><video src=\"" + _videoSource + "\"></video></item>");

I can generate and track newNode just fine, but whenever I try to add it with insertChildBefore, it always returns undefined.

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

contentNodealways excellent, but when trying insertChildBeforeor insertChildAfterhe always fails. It is strange if I make it contentNodeless specific (e.g. xml.mainSection), then it works as expected.

Thanks for any help, it drives me crazy!

+3
source share
1 answer

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

  • xml item node, . insertChildBefore xml node, contentNode .

  • contentNode, , - node, ; XML-.

:

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

Source: https://habr.com/ru/post/1698120/


All Articles