views:

1037

answers:

3

Hi,

I need to read xml tags and its datas from one file and then write it to another xml..how to do it?? please let me know immediately...?

+1  A: 

See http://livedocs.adobe.com/flex/2/langref/XML.html . I find it hard to believe you googled this before asking.

Matthew Flaschen
A: 

You can use the FileReference.save() method to save XML data to a local file. It will prompt the user for a location to save the file first and then save the data.

Here's an example:

var xml:XML = <root><someXmlTag/></root>;
var fileReference:FileReference = new FileReference()
fileReference.save(xml, "myfile.xml");
Ben Johnson
A: 

As far as I knew, Flex wasn't able to write to files!

I use a HTTPService to load the XML file and a result even handler to access it.

<mx:HTTPService id="service" url="myXml.xml" result="ServiceResult (event)"/>

Do not specify a result format in the HTTPService tag. This is the code for the result event handler.

private function ServiceResult (e : ResultEvent) : void {
    e.result.XmlTag.AnotherXmlTag;
}

You can also use service.lastResult to access the last result returned by the HTTPService. The result is fully compatible with the dataProvider property, especially in arrays and chart series.

var series : LineSeries = new LineSeries ();
series.dataProvider = e.result.XmlTag.AnotherXmlTag;

This will take the value in all AnotherXmlTag tags within XmlTag. For series, though, you should also specify either a yField or and xField, but it digress :-)

If it doesn't work, you can also cast it using the as keyword, example:

series.dataProvider = e.result.XmlTag as ArrayCollection;

I haven't actually tried casting it in this scenario, but the bottom line is that XML tags are vary compatible with arrays and ArrayCollections.

In your case, you would just use e.result to get the complete XML file, assign it to a variable, and write it using Ben's method. You can also create an array from individual values using the way I explained above, and manually insert tags and such if you need. The advantage of this is that you have all the values ready in an array would you need them later on. If you loop through the indices, this won't require a lot of work, and it would be the way I'd do it.

Hope this helps!

Aethex