如何过滤掉 XML 的特定节点?
以这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您的 XML 位于变量 myXML 中,您可以使用这样的递归函数。通过这种方式,您将不会受到元素名称(即祖父母、父母、孩子)的束缚,并且不会受到级别数量的限制(即您可以添加
)。
节点到每个
节点。)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.)这就是我能想到的,但我不喜欢循环。如果您有更好的方法,请告诉我:
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: