使用FLEX删除不需要的XML节点

时间:2012-08-17 03:31:49

标签: xml flex xml-parsing

我有一个当前正在接收XML的FLEX应用程序。我需要通过摆脱不需要的节点来清理它,但要保留他们的孩子。我尝试了各种删除组合,但我总是最终删除孩子。

我得到的是什么:

<cmisObject folder="true" name="documentLibrary">
  <children>
    <cmisObject folder="true" name="Test Folder">
      <children>
        <cmisObject folder="false" name="Test Word Doc.doc">
          <children/>
        </cmisObject>
        <cmisObject folder="false" name="Test Excel.xls">
          <children/>
        </cmisObject>
        <cmisObject folder="true" name="Test SubFolder">
          <children>
            <cmisObject folder="false" name="TestSubFolderDoc.doc">
              <children/>
            </cmisObject>
          </children>
        </cmisObject>
      </children>
    </cmisObject>
    <cmisObject folder="false" name="TestMainDoc.doc">
      <children/>
    </cmisObject>
  </children>
</cmisObject>

我想将其更改为:

<cmisObject folder="true" name="documentLibrary">
    <cmisObject folder="true" name="Test Folder">
        <cmisObject folder="false" name="Test Word Doc.doc">
        </cmisObject>
        <cmisObject folder="false" name="Test Excel.xls">
        </cmisObject>
        <cmisObject folder="true" name="Test SubFolder">
            <cmisObject folder="false" name="TestSubFolderDoc.doc">
            </cmisObject>
        </cmisObject>
    </cmisObject>
    <cmisObject folder="false" name="TestMainDoc.doc">
    </cmisObject>
</cmisObject>

对此有何想法?

1 个答案:

答案 0 :(得分:1)

嗨以下代码可以帮助您: -

我尝试了一些解决方法来实现您的目标: -

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <fx:XML id="sampleXML" format="e4x" >
            <cmisObject folder="true" name="documentLibrary">
                <children>
                    <cmisObject folder="true" name="Test Folder">
                        <children>
                            <cmisObject folder="false" name="Test Word Doc.doc">
                                <children/>
                            </cmisObject>
                            <cmisObject folder="false" name="Test Excel.xls">
                                <children/>
                            </cmisObject>
                            <cmisObject folder="true" name="Test SubFolder">
                                <children>
                                    <cmisObject folder="false" name="TestSubFolderDoc.doc">
                                        <children/>
                                    </cmisObject>
                                </children>
                            </cmisObject>
                        </children>
                    </cmisObject>
                    <cmisObject folder="false" name="TestMainDoc.doc">
                        <children/>
                    </cmisObject>
                </children>
            </cmisObject>
        </fx:XML>
    </fx:Declarations>

    <fx:Script>
        <![CDATA[

            private function parseHandler():void
            {
                var newXMLString:String = removeElements(String(sampleXML));
                var responseXML:XML = new XML(newXMLString);
                trace(responseXML)
            }

            private function removeElements(inputXml:String):String
            {
                inputXml = inputXml.split('<children>').join('');
                inputXml = inputXml.split('</children>').join('');
                inputXml = inputXml.split('<children/>').join('');
                return inputXml;
            }

        ]]>
    </fx:Script>

    <s:Button id="btnID" label="Parse XML" click="parseHandler()"/>

</s:Application>

OutPut: -

<cmisObject folder="true" name="documentLibrary">
  <cmisObject folder="true" name="Test Folder">
    <cmisObject folder="false" name="Test Word Doc.doc"/>
    <cmisObject folder="false" name="Test Excel.xls"/>
    <cmisObject folder="true" name="Test SubFolder">
      <cmisObject folder="false" name="TestSubFolderDoc.doc"/>
    </cmisObject>
  </cmisObject>
  <cmisObject folder="false" name="TestMainDoc.doc"/>
</cmisObject>