views:

235

answers:

2

Hi all , how to copy one xml object values from one xml object to another empty xml object.

I have one xml object from the xml array and need to copy that to another xml object. How can i copy xml from one object to another

if am parsing the XML object with for loop and i get the nodes

var myXML:xml = new xml();
for(...)
if(xmlObj.product[i].name == 'myproduct'){
  /// copy this to 'myXML' xml object .. how??


}

trace(myXML)
+1  A: 

try something like:

var newXml:XML = new XML(oldXml.toXMLString());
shortstick
let me try this
coderex
how to copy the specific childs only.
coderex
please check my edit
coderex
then use var newXml:XML = new XML(xmlObj.product[i]);
shortstick
this will only work if the xml is a single element (eg one root tag) otherwise you need to start using xmlList objects, see http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm
shortstick
+2  A: 

This is how I would probably do this, using AS3's E4X capabilities.

private function copyMyProductXMLNodes():void
{
    var xmlObj:XML = <productList><product name="notMyProduct">product 1</product><product name="myProduct">product 2</product><product name="notMyProduct">product 3</product><product name="myProduct">product 4</product></productList>;
    var myXML:XML = <myProductList></myProductList>;

    for each(var productItem:XML in xmlObj.product)
    {
     if(productItem.@name == 'myProduct')
     {
        myXML.appendChild(productItem.copy());
      }
    }

    trace(myXML.toXMLString())
}

I instantiated the myXML var with an XML literal, rather than leaving it as new XML() because the appendChild method can't append anything to an XML object until it has a top-level node.

I'd be glad to add a little more commentary on this code, if that would be helpful. Just let me know.

Ross Henderson