Copy XML array from one XML Object to another xml Object
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 =开发者_Go百科= 'myproduct'){
/// copy this to 'myXML' xml object .. how??
}
trace(myXML)
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.
try something like:
var newXml:XML = new XML(oldXml.toXMLString());
精彩评论