# AS3 - XML : How to add element nodes

**URL:** <https://forum.kirupa.com/t/as3-xml-how-to-add-element-nodes/280135>\
**Category:** Uncategorized\
**Created:** [January 23, 2009, 7:07am UTC](https://forum.kirupa.com/t/as3-xml-how-to-add-element-nodes/280135 "2009-01-23T07:07:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hazelvine](https://avatars.discourse-cdn.com/v4/letter/h/ecd19e/32.png) [@Hazelvine](https://forum.kirupa.com/u/Hazelvine)\
**Post date:** [January 23, 2009, 7:07am UTC](https://forum.kirupa.com/t/as3-xml-how-to-add-element-nodes/280135/1 "2009-01-23T07:07:09Z")

</div>

I’m trying to figure out how to add an element node to my XML, but can’t seem to figure out how to do it.

i.e.

var zoo:XML = \<zoo\>  
\<section\>  
\<animals\>  
\<monkeys\>Monkey1\</monkeys\>  
\<monkeys\>Monkey2\</monkeys\>  
\<zebra\>Zebra1\</zebra\>  
\</animals\>  
\</section\>  
\<section\>  
\<animals\>  
\<monkeys\>Monkey1\</monkeys\>  
\<zebra\>Zebra1\</zebra\>  
\<zebra\>Zebra2\</zebra\>  
\</animals\>  
\</section\>  
\</zoo\>

I want to add another section at the beginning.  
The results look like this:

\<zoo\>  
\<section\>  
\<animals\>  
\<zebra\>Zebra1\</zebra\>  
\<zebra\>Zebra2\</zebra\>  
\<zebra\>Zebra3\</zebra\>  
\</animals\>  
\</section\>  
\<section\>  
\<animals\>  
\<monkeys\>Monkey1\</monkeys\>  
\<monkeys\>Monkey2\</monkeys\>  
\<zebra\>Zebra1\</zebra\>  
\</animals\>  
\</section\>  
\<section\>  
\<animals\>  
\<monkeys\>Monkey1\</monkeys\>  
\<zebra\>Zebra1\</zebra\>  
\<zebra\>Zebra2\</zebra\>  
\</animals\>  
\</section\>  
\</zoo\>

I’ve tried using this code, but it ended up copying all the element nodes rather than just one.

var myCopy:XMLList = zoo.section.copy();  
myCopy[0].animals.zebra = “ZebraNew”; // 1.  
myCopy[0].animals[0] += \<zebra\>“Zebra2”\</zebra\>; // 2.  
zoo.insertChildBefore(zoo.section, myCopy);

The following code gave me this result:

\<section\>  
\<animals\>  
\<monkeys\>Monkey1\</monkeys\>  
\<monkeys\>Monkey2\</monkeys\>  
\<zebra\>ZebraNew\</zebra\> // 1.  
\</animals\>  
\<zebra\>“Zebra2”\</zebra\> // 2.  
\</section\>

1. This just copies over my original zoo.section[0].animal.zebra[0] (which was Zebra1), but it
2. Wasn’t even nested within the animals element node.

Any help or suggestions at this point would be greatly appreciated!

Thanks,

Hazel (\>\_\<)
