Introduction to XML in Flash
by senocular
A Better Understanding Of The ChildNodes
Array
When it comes to editing XML, if there's one
thing to be wary of, it's the childNodes array.
It seems simple enough but it can cause a whole
lot of confusion when you are trying to manage
and alter your XML data. The reason is because
a childNodes array is not the actual construct
of the internal workings of an XML object used
to hold XML nodes. It is merely a representation.
It contains valid node references but altering
a childNodes array will not effect the the XML
from which it came. For example, putting:
- node.childNodes[0]
= node.childNodes[1];
will do absolutely nothing despite your thoughts
that you may have just copied the contents of
childNodes[1] into childNodes[0]. Why is this
so you ask? It has to do with what the childNodes
property really is.
As you know, the childNodes property for any
given XMLNode instance (from within an XML instance)
provides you with an array of the nodes contained
within that node. However, this array is a copy
- a copy of whatever internal array or structure
is used to truly represent the list of children
within any element. So rather than thinking
of childNodes as a direct property of an element,
think of it more as an instruction or a function
call that returns a new array with XMLNode references
of that element's children. Because of this
any change to an XML array will not be reflected
in the actual XML. A loose internal representation
of what the childNodes property does as a function
may look like this:
- function childNodes(){
- var children
= new
Array();
- for (child_elements
in this_element)
{
- children.push(
this_element[child_element]
);
- }
- return children;
- }
As a function, childNodes here creates a new
array and adds child elements of the current
element into the array one by one for each contained
within. That array is then returned and given
to the childNodes property.
Now, that having been said, it's important
to understand that each XMLNode within
a childNodes array is directly related
to the XML. So adding a or changing an attribute
within an element of a child nodes array will
actually change the internal XML itself since
you're dealing with the real XML node. It's
just when you're talking about the array elements
directly, their order and their direct (non
referential) values. For instance, sorting an
array assigned to equal a childNodes array will
sort the array but have no affect on the order
of the elements within the XML as the following
diagram shows.
[
sorting a childnodes array wont affect the xml
]
Though the array itself has rearranged its
order, the organization of the actual XML remains
unchanged. This actually can be a good thing
since sorting an array is a lot easier than
sorting XML. If you are keeping a sortable list
of XML data, using an array to order that data
is a lot easier than sorting the XML nodes individually.
The Array object in Flash provides a sort function
to ease this process. No such method exists
for XML.
|