Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何使父节点成为它的子节点';E4X中的子节点是什么?_Xml_Actionscript 3_E4x - Fatal编程技术网

Xml 如何使父节点成为它的子节点';E4X中的子节点是什么?

Xml 如何使父节点成为它的子节点';E4X中的子节点是什么?,xml,actionscript-3,e4x,Xml,Actionscript 3,E4x,如何获取父节点并使其成为自己子节点的子节点,同时使其成为其子节点的父节点 例如,我有以下XML: <root> <range> <a> <b> <c> </range> <range> <a> <b> <c> <range> <bleep> <range>

如何获取父节点并使其成为自己子节点的子节点,同时使其成为其子节点的父节点

例如,我有以下XML:

<root>
  <range>
    <a>
    <b>
    <c>
  </range>
  <range>
    <a>
    <b>
    <c>
  <range>
  <bleep>
      <range>
        <a>
        <b>
        <c>
      </range>
      <range>
        <a>
        <b>
        <c>
      </range>
  </bleep>
</root>

我希望它看起来像这样:

<root>
  <range>
    <a>
    <b>
    <c>
  </range>
  <range>
    <a>
    <b>
    <c>
  <range>
  <range>
     <bleep>
        <a>
        <b>
        <c>
     </bleep>
  </range>
  <range>
     <bleep>
        <a>
        <b>
        <c>
     </bleep>
  </range>
</root>

以下代码交换bleep和范围父子关系:

var xml:XML = myXML.copy();
trace(xml);

const CharacterStyleRange:String = "range";
const HyperlinkTextSource:String = "bleep";

var children:XMLList = xml.children();
var newChildren:XMLListCollection = new XMLListCollection();

// loop through paragraph children
for each (var child:XML in children) {
    // if CharacterStyleRange
    if (child.name() == CharacterStyleRange) {
        newChildren.addItem(child);
    }

    // if HyperlinkTextSource
    // get ranges and insert hyperlink text source
    else if (child.name() == HyperlinkTextSource) {
        var ranges:XMLList = child.CharacterStyleRange;

        for each (var range:XML in ranges) {
            var rangeChildren:XMLList = range.children();
            var newChild:XML = child.copy();
            newChild.setChildren(rangeChildren);
            range.setChildren(newChild);
            newChildren.addItem(range);
        }
    }
}

var newChildrenXML:XMLList = newChildren.copy();
xml.setChildren(newChildrenXML);
trace(xml);

我认为您应该构造一个新的XML,其中包含要放置的所有节点的直接子节点,而不是节点,然后向每个子节点添加一个与替换节点同名的节点,然后将所有其他子节点添加到添加的节点中。或者,如果E4X允许,则从原始XML中提取要替换的节点,并对现有XML执行相同的操作。它是由一个非常流行的程序导出的格式,因此将来不太可能更改。