Skip to main content
Inspiring
August 13, 2018
Question

Accessing XML nodes through attributes.

  • August 13, 2018
  • 1 reply
  • 447 views

This is my XML:

<root>

  <terms>

     <word type="term1" value="Definition for term1."/>

     <word type="term2" value="Definition for term2."/>   

     <word type="term3" value="Definition for term3."/>

     <word type="term4" value="Definition for term4."/>

  </terms>

</root>

To access particular node I use the following:

var definition:XMLList = myXml.terms.word.@type;

This allows me to easily access any node just by typing: definition

So, far I've built a list of words consisting of attributes, and if I click on of them, it displays the "value" attibute.

However, I'd like to add a search option, and this is where I have some problems. I know I can find an attribute with the following:

var definition:XMLList = myXml.terms.word.(@type == "term3");

But how do I retrieve appropriate "value" attribute corresponding to that "type" attribute? Is there any way to get the number of this node, so I could use then something like valueXMLList, or maybe there's a different, better way?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
August 13, 2018

function completeF(e:Event):void{

var myXml=XML(e.target.data);

nodeF(myXml.terms.word.(@type=='term3'));

}

function nodeF(list:XMLList):void {

var item:XML;

for each(item in list) {

trace(item.@value);

}

}