1 Reply Latest reply on Mar 4, 2008 2:36 PM by Peter deHaan

    Array to XML

    dmars87
      I have successfully created an HTTPService that hydrates an ArrayCollection. This ArrayCollection is the data provider for an mx:DataGrid. After the user has sorted or filtered records or rows of the datagrid, I would like to extract the filtered or sorted data as XML. The ulitmate goal here is the sorted or filtered XML would be the source data for a dump into Excel. I know that I can do this by requerying the HTTPService with all the sort and filter parameters, but I was hoping there was an easy way to do something like:

      dataGrid.dataProvider.source.toXml() ???
        • 1. Re: Array to XML
          Peter deHaan Level 4
          dmars87,

          You may want to try playing with the SimpleXMLEncoder class (mx.rpc.xml.SimpleXMLEncoder).

          Something like this may work (or at least get you started):
          [code]
          import mx.rpc.xml.SimpleXMLEncoder;

          private function objectToXML(obj:Object):XML {
          var xmlDocument:XMLDocument = new XMLDocument();
          var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
          var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, new QName("root"), xmlDocument);
          return new XML(xmlDocument.toString());
          }
          [/code]

          Basically the code takes an object (myArrayCollection.source -- an Array, for example) and uses the SimpleXMLEncoder class to convert that object into an XMLDocument object. Finally, you convert the XMLDocument to an XML object using the XML constructor and XMLDocument.toString() method.

          Peter