How do I copy an XMLList and keep the namespace?

95 Views Asked by At

Is there a way to copy an XMLList and keep the namespace? It seems when I use the copy() method on an XMLList the namespace is being removed.

Code:

var xml:XML = <s:Label xmlns:s="library://ns.adobe.com/flex/spark"><s:color>red</s:color>
        test level</s:Label>;

var elementsXMLList:XMLList = xml.elements();
trace(elementsXMLList.toXMLString()); // has namespaces

var elementsCopy:XMLList = elementsXMLList.copy();
trace(elementsCopy.toXMLString()); // does not have namespaces

elementsXMLList.toXMLString():

<s:color xmlns:s="library://ns.adobe.com/flex/spark">red</s:color>

elementsCopy().toXMLString():

<color xmlns="library://ns.adobe.com/flex/spark">red</color>

I need to be able to keep the namespace when I make the copy.

1

There are 1 best solutions below

0
Michael Antipin On

This must be because it is s:Label node that contains the namespace declaration. xml.elements() gets you a list of xml elements that are part of s:Label's tree, so they do know their namespace. When you do elements.copy() though you get a separate list of XML elements that are no longer part of the tree that has xmlns:s.

I'd suggest that you copy the whole tree starting with s:Label, then get its elements, instead of doing it another way around.