# Assign XML node values dependent on "id" attribute in the XML file

**URL:** <https://forum.kirupa.com/t/assign-xml-node-values-dependent-on-id-attribute-in-the-xml-file/185622>\
**Category:** Uncategorized\
**Created:** [April 14, 2006, 7:21pm UTC](https://forum.kirupa.com/t/assign-xml-node-values-dependent-on-id-attribute-in-the-xml-file/185622 "2006-04-14T19:21:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bchalker](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bchalker/32/441_2.png) [@bchalker](https://forum.kirupa.com/u/bchalker)\
**Post date:** [April 14, 2006, 7:21pm UTC](https://forum.kirupa.com/t/assign-xml-node-values-dependent-on-id-attribute-in-the-xml-file/185622/1 "2006-04-14T19:21:48Z")

</div>

Here is what I am needing to do:  
I have anywhere from 8-20 MCs (objects) on a screen. Each may or may not be visible, and each will be in a different location, independent of other MCs. The XML will assign values (X,Y, alpha, rotation, loadMovie, color, etc.) to each clip.

I know how to hard code each MC to the XMLNode in the file, but am wanting to have the id attribute (“id=object1”, “id=object2”, etc.) to determine which nodes/children to pull values from. IE, my MC called “quoteOfTheDay”, I want to read the values from the XML node with the id=“quoteOfTheDay”. And so on…

Does anyone have an idea how to do this? I’m thinking a loop that pulls all attributes named id, or something?

anyone…

thanks

my XML  
////////////////////////////////////////////  
\<objects backgroundImage=“bg1.jpg” \>  
\<object id=“object1”\>  
\<visible\>1\</visible\>  
\<xPos\>80\</xPos\>  
\<yPos\>40\</yPos\>  
\<alpha\>80\</alpha\>  
\<textContent\>Number 1\</textContent\>  
\</object \>  
\<object id=“object2”\>  
\<visible\>1\</visible\>  
\<xPos\>400\</xPos\>  
\<yPos\>40\</yPos\>  
\<alpha\>20\</alpha\>  
\<textContent\>Number 2\</textContent\>  
\</object \>  
\<object id=“object3”\>  
\<visible\>1\</visible\>  
\<xPos\>500\</xPos\>  
\<yPos\>130\</yPos\>  
\<alpha\>80\</alpha\>  
\<textContent\>Number 3\</textContent\>  
\</object \>

\</objects\>

////////////////////////////////////////////

---

<div class="post-metadata">

**Author:** ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)\
**Post date:** [April 14, 2006, 9:08pm UTC](https://forum.kirupa.com/t/assign-xml-node-values-dependent-on-id-attribute-in-the-xml-file/185622/2 "2006-04-14T21:08:35Z")

</div>

This looks like a job for idMap…

## [AS]var props\_xml:XML = new XML(); var props\_obj:Object = new Object(); // props\_xml.ignoreWhite = true; props\_xml.onLoad = function(success) { if (success) { var temp\_xml = new XML(); temp\_xml.ignoreWhite = true; temp\_xml.parseXML(props\_xml.toString()); props\_obj = temp\_xml.idMap; // for (var props in props\_obj) { trace(props\_obj[props]); trace("

");  
}  
}  
};  
//  
props\_xml.load(“objects.xml”);[/AS]

---

<div class="post-metadata">

**Author:** ![bchalker](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bchalker/32/441_2.png) [@bchalker](https://forum.kirupa.com/u/bchalker)\
**Post date:** [April 15, 2006, 2:35am UTC](https://forum.kirupa.com/t/assign-xml-node-values-dependent-on-id-attribute-in-the-xml-file/185622/3 "2006-04-15T02:35:09Z")

</div>

Very cool! :!:

One question though…  
how do I pull from the child nodes contained in each id node?

---

<div class="post-metadata">

**Author:** ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)\
**Post date:** [April 15, 2006, 4:11am UTC](https://forum.kirupa.com/t/assign-xml-node-values-dependent-on-id-attribute-in-the-xml-file/185622/4 "2006-04-15T04:11:09Z")

</div>

each property of the props\_obj is itself an xml object. so you can access the nodes just as you would with any xml… Something like this would do the trick:  
[AS]var props\_xml:XML = new XML();  
var props\_obj:Object = new Object();  
//  
props\_xml.ignoreWhite = true;  
props\_xml.onLoad = function(success) {  
if (success) {  
var temp\_xml = new XML();  
temp\_xml.ignoreWhite = true;  
temp\_xml.parseXML(props\_xml.toString());  
props\_obj = temp\_xml.idMap;  
// attach a movie clip to test it out  
attachTest();  
}  
};  
//  
function attachTest():Void {  
var test:MovieClip = this.attachMovie(“test\_mc”, “tmc”, this.getNextHighestDepth(), {props:props\_obj.object1});  
// set properties  
test.\_visible = (Number(test.props.childNodes[0].firstChild.toString()) \> 0);  
test.\_x = Number(test.props.childNodes[1].firstChild.toString());  
test.\_y = Number(test.props.childNodes[2].firstChild.toString());  
test.\_alpha = Number(test.props.childNodes[3].firstChild.toString());  
test.someText\_txt.text = test.props.childNodes[4].firstChild.toString();  
}  
//  
props\_xml.load(“objects.xml”);[/AS]
