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>
- This just copies over my original zoo.section[0].animal.zebra[0] (which was Zebra1), but it
- Wasn’t even nested within the animals element node.
Any help or suggestions at this point would be greatly appreciated!
Thanks,
Hazel (>_<)