Generate XML data dynamically in Flex.

We may have a requirement were we wanted to generate XML data to send through Httpservice or create a XML page with it.

Then here to get that XML type data we could use a little bit of ActionScript code like below.

Here iam using XML Class and XMLList Class.

First iam defining my RootNode of type XML and then node name and node value of Type String and Then create a XMLList using this node name and node value,and then append this to XML RootNode using the method appendChild().Thats it ur XML data is created, now we can extend this to generate any number of XML lines type data.

Code Example:

private function createXML(): void
{
var xm:XML = <Relyon></Relyon>;
var nodeName:String = “EMPLOYEENAME”;
var nodeValue:String = “KUMAR”;
var xmlList:XMLList = XMLList(“<“+nodeName+”>”+nodeValue+”</”+nodeName+”>”);
xm.appendChild(xmlList);
Alert.show(xm);
}

This will do the JOB. Leave any comments for queries.

23 Responses to Generate XML data dynamically in Flex.

  1. Meena says:

    Hi Kumar,

    Thanks for the info,
    how can I generate XML like this:

    Thanks for any help.

  2. kumargandhi says:

    Hello Meena,

    For this we got to use the some classes in flash.xml package, i can give another example for you.

    [Bindable]
    public var xml:XML

    private function createXML():void

    {
    try
    {
    xml =

    {content.text}
    ;
    }
    catch (e:TypeError)

    {
    xml = Fill the form to see the tag here.;
    }

    }

    by this also we can generate dynamic xml data,By using XML class.
    here tagName , attributeName are my TextInput Components.

    regards,
    kumar.

  3. Balaji says:

    Hi Kumar,
    Can you tell me how to bind the data of xml in the class file.
    i mean to say i will be creating the class file there i need to get the xml data for manipulating.

    Let me know if u have any idea about this
    Thanks and Regards
    Balaji

  4. kumargandhi says:

    Hi,
    I will come up with a post on this soon.

    regards,
    kumar

  5. […] XML Data dynamically in Flex :: Reloaded Few months back i made a post on this “Generate XML Data dynamically in Flex” and it needed some improvements. so here i am with the improvements. [ As few readers suggested […]

  6. Jagdish says:

    Want dynyamic XML

  7. kumargandhi says:

    There are some methods in the XML class you can use those, here to get XML there is one method called toXMLString(), this will give out the XML in String format.try it.
    or use the continuation post for this post,

    Generate XML Data dynamically in Flex :: Reloaded

    regards,
    kumar.

  8. raj says:

    how to display & there……

    i tried with & but its not working………….

  9. kumargandhi says:

    Hi,

    It will take that character, i don’t understand your problem, can you be clear.

    regards,
    kumar.

  10. raj says:

    How to add ampercent in xml node…….
    this is the sample code…….
    here i am giving & but its displaying as &amp;

    <![CDATA[
    import mx.controls.TextArea;
    import mx.controls.Text;
    import mx.controls.Alert;
    private function createXML(): void
    {
    var xm:XML = ;
    var nodeName:String = “EMPLOYEENAME”;
    var nodevaule:Text=new Text();
    nodevaule.text=”&”;
    var xmlList:XML = {nodevaule.text};
    xm.appendChild(xmlList);
    Alert.show(xm.toXMLString());
    }

    ]]>

  11. raj says:

    <![CDATA[
    import mx.controls.TextArea;
    import mx.controls.Text;
    import mx.controls.Alert;
    private function createXML(): void
    {
    var xm:XML = ;
    var nodeName:String = “EMPLOYEENAME”;
    var nodevaule:Text=new Text();
    nodevaule.text=”&”;
    var xmlList:XML = {nodevaule.text};
    xm.appendChild(xmlList);
    Alert.show(xm.toXMLString());
    }

    ]]>

  12. Ravindra says:

    hi,

    i wanted to write data in a exisiting xml file.

    Please suggets

  13. kumargandhi says:

    Hi Ravindra,

    plz follow the link below about your question, and note the second comment on the post.

    Generate XML Data dynamically in Flex :: Reloaded

    regards,
    kumar.

  14. mayur says:

    Thanks.
    I got hit.
    That makes my work done.

    Mayur

  15. Dileep Kumar says:

    Hi Kumar,
    I am using the same code what you are using for generating xml list.But the problem with me is that I have to generate bulk amount of data.I am running a for loop and inside that xml is getting generated based on the length of the array.
    So for bulk amount of data its taking too much amount of time and application is getting hanged.
    Is there any alternate way to generate xml in a fast way?Please tell me .

  16. […] Generate XML data dynamically in Flex. […]

  17. Mike Kelly says:

    Hi,
    I was trying to add an XML node with the same name as already existing nodes, but with different attributes. None of the books I have indicated (or at least I couldn’t find the information) that I could add the node as a quoted string, thus allowing the addition of the attributes as variables.

    Anyway, figured it out with the help of your site.
    Thanks,
    Mike

  18. taqui says:

    Suppose there are names and phone numbers stored in a xml file. I want to retrieve phone number when particular name is selected

  19. Ashwini says:

    Hi…
    Am new to Flex, I need to work with java class and flex, can u help me out in this. how do i integrate java with flex first.

    Regards
    Ashwini

  20. Sudharsanan says:

    How to append the data to external xml file which was stored in different location.

  21. rohit says:

    How can I add attributes in the xml node

  22. […] months back i made a post on this “Generate XML Data dynamically in Flex” and it needed some improvements. so here i am with the improvements. [ As few readers suggested ] […]

Leave a comment