0

I need to add an attribute to all nodes in an XML dynamically.My xml is as follows:

   <root>
     <item>
        <item>Americas</item>
        <item>Canada</item>
        <item>Ottawa</item>
        <item>Category 1</item>
        <item>Product 01</item>
        <item>4171.132339235787</item>
        <item>4181.132339235787</item>
      </item>
   </root>

For which I need to add an attribute named "name" to each node in above XML as:

<root>
 <item name="">
   <item name="Americas"/>
   <item name="Canada"/>
   <item name="Ottawa"/>
   <item name="Category 1"/>
   <item name="Product 01"/>
   <item name="4171.132339235787"/>
   <item name="4181.132339235787"/>
 </item>
</root>

How can this be achieved in Flex XML?

1 Answer 1

1

You need an attribute named label? But your final XML does not have any such attributes. Anyways you can use the following to add the "name" attribute to each child element of the xml:

 <mx:Script>
            <![CDATA[
                private var newLoad:URLLoader;
                private var link:String = "xl.xml";
                private var req:URLRequest = new URLRequest(link);
                loadU();//Place this call in the creation complete handler of the Application's CreationComplete Event
                private function load(e:Event):void
                {
                    var xm:XML = XML(e.target.data);
                    for each(var node:XML in xm.item.item)
                    {
                        node.@name = node;
                    }
                    var file:FileReference = new FileReference();
                    file.save(xm,"x1.xml");//Save the output file
                }
                private function loadU():void
                {
                    newLoad = new URLLoader();
                    newLoad.addEventListener(Event.COMPLETE,load);
                    newLoad.load(req);
                }
            ]]>
        </mx:Script>

Let me know if this is what you want.

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

4 Comments

thanks for the answer.Yes this is what I needed.But One thing is that the immediate node after the "Root" tag does not have the attribute "name". How to get it at that level too?
i guess you can use a seperate "for" loop for adding the attribute and in that loop you can traverse down till the first node using the below: for each(var node:XML in xm.item) //traversing to first child of the root node instead of for each(var node:XML in xm.item.item) //traversing till the first grandchild of the root node hope it helps!
Am unable to add an attribute to the child nodes like how you did for grand child nodes.How to do this.I had used for each(var node:XML in xm.item) { for(var j:int=0; j<valueXml.length(); j++) { node.@name = xm.item[j].item[0].@name; } }
This should work: for(var i:int=0; i<xm.length(); i++) { xm[i].item.@name = "name"; }

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.