Trouble with insertChildAfter for xml in actionsctipt 3

僤鯓⒐⒋嵵緔 提交于 2019-12-24 18:53:01

问题


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>

回答1:


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.



来源:https://stackoverflow.com/questions/5942414/trouble-with-insertchildafter-for-xml-in-actionsctipt-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!