开发者

Trouble with insertChildAfter for xml in actionsctipt 3

I'm having a lot of trouble with inserting a child after an existing node that is VERY nested in my XML. I'm trying to insert another IObject node beneath object3_LIST3.

I've tried the same scheme as Having trouble with insertChildBefore and insertChildAfter in AS3 with this. This always outputs undefined and I can't seem to figure out why.. Can anyone help?

If I trace contentNode and parentNode, it outputs:

object3_LIST3

and

<objects>
  <IObject>object3_LIST3</IObject>
</objects>

As if no changes were made at all to the document...

var contentNode:XML = xml.Menu.menuArr.HeadMenuItem[1].subMenu.subMenuItem[1].objects[0];
var parentNode:XML = xml.Menu.menuArr.HeadMenuItem[1].subMenu.subMenuItem[1].objects.IObject[0];
xml = parentNode.insertChildAfter( contentNode, xmlString );
trace(xml);

<PandemicMenu>
    <Menu>
        <menuArr>
            <HeadMenuItem>
                <subMenu>
                    <IObject>
                        object1_LIST1
                    </IObject>
                    <IObject>
                        object2_LIST1
                    </IObject>
                    <IObject>
                   开发者_高级运维     object3_LIST1
                    </IObject>
                </subMenu>
            </HeadMenuItem>

            <HeadMenuItem>
                <subMenu>
                    <IObject>
                        object1_LIST2
                    </IObject>
                    <subMenuItem>
                        <objects>
                            <IObject>
                                object2_LIST2
                            </IObject>
                        </objects>
                    </subMenuItem>
                    <subMenuItem>
                        <objects>
                            <IObject>
                                object3_LIST3
                            </IObject>
Want to insert new IObject here
                        </objects>
                    </subMenuItem>
                </subMenu>
            </HeadMenuItem>
        </menuArr>
    </Menu>
</PandemicMenu>


Your parent node and child nodes are switched. You child node is:

var contentNode:XML = xml.Menu.menuArr.HeadMenuItem[1].subMenu.subMenuItem[1].objects[0];

Which would trace the following with a contentNode.toXMLString():

<objects>
  <IObject>object3_LIST3</IObject>
</objects>

Since the docs state: "If child1 is provided, but it does not exist in the XML object, the XML object is not modified and undefined is returned." http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/XML.html#insertChildAfter%28%29

The problem could be that you are asking the child node to look for its parent node, which it can't find, and so the original is unmodified. Try the following:

var parentNode:XML = xml.Menu.menuArr.HeadMenuItem[1].subMenu.subMenuItem[1].objects[0];
var contentNode:XML = xml.Menu.menuArr.HeadMenuItem[1].subMenu.subMenuItem[1].objects.IObject[0];
xml = parentNode.insertChildAfter( contentNode, xmlString );
trace(xml);

All I did was switch the content of the parentNode and contentNode XML objects.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜