문제

I've got an XML file that has many nodes along the lines of this:

<animal name="dog">
    ...
    <tag>mammal</tag>
    <tag>brown</tag>
</animal>

I'm hoping to use the tag node as part of a search function for certain animals. So I could collate a list of all mammals for example.

I've been playing around with something along the lines of this:

myXML..tag=="mammal" or myXML.*.tag=="mammal"

Also tried switching tag to have an attribute (eg: <tag type="brown"/>), and then searching using something like myXML..tag.@type=="brown", but alas, both methods trace out to something useless like "false".

I don't particularly want to have to loop through every node to try and find this, as I have too many that need to be searched quite often. So if there was some magical way (might not have to be XML) that I can enter a search value or tag, and it return a list or array of every animal.@name that has the specified tag.

This has kind of been asked before, but not quite like this and never really answered (that I could find).

도움이 되었습니까?

해결책

Try with this one,

var xml:XML = <animal name="dog">               
        <tag type="mammal">mam</tag>
        <tag type="brown">br</tag>                      
        <tag type="black">bl</tag>                      
        <tag type="black">black</tag>
      </animal>;

var attr:String = "black";              
var val:String = "b";
var resultList:XMLList = xml..tag.(@type == attr).(toString().indexOf(val)>-1);

o/p:

<tag type="black">
  bl
</tag>
<tag type="black">
  black
</tag>

Anyhow you always use loop to get better performance.e4X Slow. Read carefully 15 point.

Based on your comment:

var myXML:XML = <animal name="dog">                                     
                         <tag type="mammal">true</tag>                  
                         <tag type="brown">true</tag>                   
                         <tag type="black">true</tag>                                   
                    </animal>;                  

                for each(var tag:XML in myXML..tag) //no matter of depth Note here
                {
                    if (tag.@type == "mammal") 
                    { 
                        trace(tag.parent().@name); 
                        trace(tag.text()); 
                    } 
                }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top