Copy link to clipboard
Copied
Hi People,
How to find the parent node of child node in xml using as3?
My XML structure
<video id="" name="test">
<subVideo id="" name="test1" src="video.mp4" content="video" />
<subVideo id="" name="test2" src="video.mp4" content="video" />
</video>
I want to get parent node name of subVideo node.
Please help me.
Thank you people.
Copy link to clipboard
Copied
the parent's node name is video.
do you mean you want to get the node itself or it's id or name given a particular subvideo id or name or src?
Copy link to clipboard
Copied
Assuming you have already created your XML object in Flash and have a reference to one of the children (<subVideo> tag in the example you provided) as either an XML object or XMLList object, you can call the .parent() method to get a reference to the parent of the <subVideo> tag and then call the .attribute() method passing "name" into the method call and it will return a String of "test". Below is the code example I created real quick. It assumes that you have an XML file named "test.xml" in the same directory as the FLA/SWF. The "test.xml" file I created for testing contains the exact example you provided.
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var xml:XML;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onFileLoad);
urlLoader.load(new URLRequest("test.xml"));
function onFileLoad(e:Event):void {
xml = new XML(e.target.data);
// xml.children() = XMLList object of the two <subVideo> tags
// xml.children().parent() is, in this case, an XML object identical to the "xml" variable
// xml.childnre().parent().attribute("name") returns the String value of the "name" attribute
trace(xml.children().parent().attribute("name"));// returns String value of "test"
}
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html
Find more inspiration, events, and resources on the new Adobe Community
Explore Now