我如何筛选出的XML secific节点? [英] How do I filter out secific nodes of XML?

查看:251
本文介绍了我如何筛选出的XML secific节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拿这个XML例子:

<root>
  <grandParent GPid="1" hidden="false">
    <parent Pid="1" hidden="false">
      <child Cid="1" hidden="false"/>
      <child Cid="2" hidden="true"/>
    </parent>
    <parent Pid="2" hidden="false">
      <child Cid="3" hidden="false"/>
      <child Cid="4" hidden="false"/>
    </parent>
  </grandParent>
  <grandParent GPid="2" hidden="false">
    <parent Pid="3" hidden="false">
      <child Cid="5" hidden="true"/>
    </parent>
    <parent Pid="4" hidden="true">
      <child Cid="6" hidden="false"/>
    </parent>
  </grandParent>
  <grandParent GPid="3" hidden="true">
    <parent Pid="5" hidden="false">
      <child Cid="7" hidden="false"/>
    </parent>
  </grandParent>
</root>

我需要某种过滤得到这个,所有的节​​点标有隐藏像这样被删除副本:

I need some sort of filter to get a copy of this where all the nodes marked "hidden" are removed like so:

<root>
  <grandParent GPid="1" hidden="false">
    <parent Pid="1" hidden="false">
      <child Cid="1" hidden="false"/>
    </parent>
    <parent Pid="2" hidden="false">
      <child Cid="3" hidden="false"/>
      <child Cid="4" hidden="false"/>
    </parent>
  </grandParent>
  <grandParent GPid="2" hidden="false">
    <parent Pid="3" hidden="false"/>
  </grandParent>
</root>

我试图用这样的事情

I tried using something like this

var newXML:XML = XML(root.(grandParent.@hidden != "true").(grandParent.parent.@hidden != "true").(grandParent.parent.child.@hidden !=true);

但是,这真的只是给我回原始的XML(因为我要求在满足这些条件,我得到了根本的根源)。我明白了为什么我的方法是行不通的,但我不知道何去何从。

But that really just gives me back the original XML (since I'm asking for the root where those conditions are met I get the root). I understand why my approach doesn't work, but I don't know where to go from here.

推荐答案

您可以使用递归函数像这样假设你的XML是在一个变量与myXML。这样做,这样,你就不会被捆绑到你的元素的名称(即祖父母,父母,子女),你将不会被限制在水平(即数。你可以添加一个&LT ;宠物&GT; 节点,每个&LT;孩子&GT; 节点)

You could use a recursive function like this assuming your XML is in a variable myXML. Doing it this way, you would not be tied to the name of your elements (ie. grandParent, parent, child) and you would not be restricted in the number of levels (ie. you could add a <pet> node to each <child> node.)

public function removeElements( avXml:XML, avAttributeName:String, avCondition:String) {

    var lvAttributeValue:String;
    var lvXml:XML;

    var lvXmlList:XMLList = new XMLList();
    for each( lvXml in avXml.children() ) {
        lvAttributeValue = lvXml.attribute( avAttributeName );
        if( lvAttributeValue == avCondition )
            lvXmlList += lvXml;

        avXml.setChildren( lvXmlList ); 
    }

    for each( var lvXmlChild:XML in avXml.children() ) {
        removeElements(lvXmlChild,avAttributeName,avCondition);
    } 
}


removeElements(myXML, "hidden", "false");
trace(myXML.toXMLString());


 <root hidden="false">
      <grandParent GPid="1" hidden="false">
        <parent Pid="1" hidden="false">
          <child Cid="1" hidden="false"/>
        </parent>
        <parent Pid="2" hidden="false">
          <child Cid="3" hidden="false"/>
          <child Cid="4" hidden="false"/>
        </parent>
      </grandParent>
      <grandParent GPid="2" hidden="false">
        <parent Pid="3" hidden="false"/>
      </grandParent>
    </root>

这篇关于我如何筛选出的XML secific节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆