如何过滤掉 XML 的特定节点?

发布于 2024-08-18 10:15:39 字数 1693 浏览 7 评论 0原文

以这个 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>

我需要某种过滤器来获取此副本,其中所有标记为“隐藏”的节点都被删除,如下所示:

<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>

我尝试使用类似的东西

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

但这实际上只是给了我原始的 XML(因为我'我询问满足这些条件的根,我得到了根)。我明白为什么我的方法不起作用,但我不知道从这里该去哪里。

Take this XML example:

<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);

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-08-25 10:15:39

假设您的 XML 位于变量 myXML 中,您可以使用这样的递归函数。通过这种方式,您将不会受到元素名称(即祖父母、父母、孩子)的束缚,并且不会受到级别数量的限制(即您可以添加 )。 节点到每个 节点。)

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>

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>
山田美奈子 2024-08-25 10:15:39

这就是我能想到的,但我不喜欢循环。如果您有更好的方法,请告诉我:

var newXML:XML = new XML(root);
var i:uint=0;
for(i=0;i<newXML.grandparent.parent.child.(@hidden == false).length();i++){
  delete newXML.grandparent.parent.child.(@hidden == false)[0];
  //always [0] since the list is shortened by 1 each iteration
}
for(i=0;i<newXML.grandparent.parent.(@hidden == false).length();i++){
  delete newXML.grandparent.parent.(@hidden == false)[0];
}
for(i=0;i<newXML.grandparent.(@hidden == false).length();i++){
  delete newXML.grandparent.(@hidden == false)[0];
}

So here's what I was able to come up with, but I don't like having to loop. Let me know if you have a better way:

var newXML:XML = new XML(root);
var i:uint=0;
for(i=0;i<newXML.grandparent.parent.child.(@hidden == false).length();i++){
  delete newXML.grandparent.parent.child.(@hidden == false)[0];
  //always [0] since the list is shortened by 1 each iteration
}
for(i=0;i<newXML.grandparent.parent.(@hidden == false).length();i++){
  delete newXML.grandparent.parent.(@hidden == false)[0];
}
for(i=0;i<newXML.grandparent.(@hidden == false).length();i++){
  delete newXML.grandparent.(@hidden == false)[0];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文