xml 如何通过flex / actionscript中的属性名称/值查找特定的xml数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2225864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:58:08  来源:igfitidea点击:

how to find specific xml data by attribute name/value in flex / actionscript

xmlapache-flexsearchactionscript

提问by Scott Szretter

From some xml I want to find items that have a specific attribute and value.

从一些 xml 我想找到具有特定属性和值的项目。

Here is example xml:

这是示例xml:

<node>
 <node>
  <node>
   <special NAME="thisone"></special>
  </node>
  <node>
   <special>dont want this one</special>
  </node>
 </node>
</node>

(nodes can contain nodes...)

(节点可以包含节点...)

I need to find the first based on it has an attribute named "NAME" and value of "thisone".

我需要根据它找到第一个具有名为“NAME”的属性和“thisone”的值。

then I need its parent (node).

然后我需要它的父节点(节点)。

I tried this:

我试过这个:

specialItems = tempXML.*.(hasOwnProperty("NAME"));

specialItems = tempXML.*.(hasOwnProperty("NAME"));

but didnt seem to do anything.

但似乎没有做任何事情。

??

??

Thanks!

谢谢!

回答by Michael Brewer-Davis

In ActionScript you'll use E4X rather than XPath, generally. What you want can be achieved like this:

在 ActionScript 中,您通常会使用 E4X 而不是 XPath。你想要的可以这样实现:

var xml:XML = <node>...</node>;
var selected:XMLList = xml.descendants().(attribute("NAME") == "thisone");      
var first:XML = selected[0];
var parent:XML = first.parent();

If you know the node you want is a special, then you can use:

如果你知道你想要的节点是 a special,那么你可以使用:

var selected:XMLList = xml..special.(attribute("NAME") == "thisone");

instead. Here's a nice E4X tutorial.

反而。这是一个不错的 E4X 教程

If you use the @NAME == "thisone"syntax, then you do need the NAME attribute on all of your XML nodes, but not if you use the attribute()operator syntax instead.

如果您使用该@NAME == "thisone"语法,那么您确实需要所有 XML 节点上的 NAME 属性,但如果您改用attribute()运算符语法则不需要。



I added the parent()call above; you could get the parent directly by using the child only in the conditional:

我在parent()上面添加了调用;您可以通过仅在条件中使用孩子来直接获得父母:

xml..node.(child("special").attribute("NAME") == "thisone");        

回答by George Profenza

You could do this in 2 ways:

你可以通过两种方式做到这一点:

  1. add the NAME attribute to all your special nodes, so you can use an E4X conditions(xml)
  2. use a loop to go through special nodes and check if there is actually a NAME attribute(xml2)
  1. 将 NAME 属性添加到所有特殊节点,以便您可以使用 E4X 条件(xml
  2. 使用循环遍历特殊节点并检查是否确实存在 NAME 属性(xml2

Here is an example:

下面是一个例子:

//xml with all special nodes having NAME attribute
var xml:XML = <node>
 <node>
      <node>
        <special NAME="thisone"></special>
      </node>
      <node>
        <special NAME="something else">dont want this one</special>
      </node>
     </node>
</node>
//xml with some special nodes having NAME attribute
var xml2:XML = <node>
 <node>
      <node>
        <special NAME="thisone"></special>
      </node>
      <node>
        <special>dont want this one</special>
      </node>
     </node>
</node>

//WITH 4XL conditional
var filteredNodes:XMLList = xml.node.node.special.(@NAME == 'thisone');
trace("E4X conditional: " + filteredNodes.toXMLString());//carefull, it traces 1 xml, not a list, because there only 1 result,otherwise should return 
//getting the parent of the matching special node(s)
for each(var filteredNode:XML in filteredNodes)
    trace('special node\'s parent is: \n|XML BEGIN|' + filteredNode.parent()+'\n|XML END|');

//WITHOUGH E4X conditional
for each(var special:XML in xml2.node.node.*){
    if([email protected]()){
        if(special.@NAME == 'thisone')  trace('for each loop: ' + special.toXMLString() + ' \n parent is: \n|XML BEGIN|\n' + special.parent()+'\n|XML END|');
    }
}

There is a pretty good and easy to follow article on E4X on the yahoo flash developer page.

yahoo flash developer page上有一篇关于 E4X 的很好且易于理解的文章。