1

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)
2
  • are you using as3 or as2? your question is tagged with both, but this specific operation is quite different between the two. Commented Dec 8, 2009 at 9:11
  • im asking about the as3 script Commented Dec 8, 2009 at 9:16

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

try something like:

var newXml:XML = new XML(oldXml.toXMLString());

4 Comments

how to copy the specific childs only.
then use var newXml:XML = new XML(xmlObj.product[i]);
this will only work if the xml is a single element (eg one root tag) otherwise you need to start using xmlList objects, see kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm
to clarify the preceding comment, it doesn't have to contain ONLY a single element, but there must only be 1 element at the root. meaning, <foo><bar/><bar/><bar/></foo> is working as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.