Flex: Convert ArrayCollection to XML

In Flex, it is easy to convert XML to Object and to ArrayCollection using

var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();
decoder.decodeXML( xml );

But is there a good way to convert ArrayCollection to XML. I searched for a while on the Internet and did not find the answer.

Thanks.

+3
source share
3 answers

Take a look at this example that uses SimpleXMLEncoder:

<mx:Script>
    <![CDATA[

        import mx.rpc.xml.SimpleXMLEncoder;
        import mx.utils.ObjectUtil;
        import mx.utils.XMLUtil;
        import mx.collections.ArrayCollection;

        private var items:ArrayCollection;

        private function onCreationComplete():void
        {
            var source:Array = [{id:1, name:"One"}, {id:2, name:"Two"}, {id:3, name:"Three"}];
            var collection = new ArrayCollection(source);

            trace(objectToXML(collection.source).toXMLString());
        }

        private function objectToXML(obj:Object):XML 
        {
            var qName:QName = new QName("root");
            var xmlDocument:XMLDocument = new XMLDocument();
            var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
            var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
            var xml:XML = new XML(xmlDocument.toString());

            return xml;
        }

    ]]>
</mx:Script>

... which creates the following XML:

<root>
  <item>
    <id>1</id>
    <name>One</name>
  </item>
  <item>
    <id>2</id>
    <name>Two</name>
  </item>
  <item>
    <id>3</id>
    <name>Three</name>
  </item>
</root>

, objectToXML , , -, SimpleXMLEncoder. ( ), , , . (, Font .)

, , . , !

+4

, toArray() ArrayCollection, SimpleXMLEncoder?

+1

descibeType(), xml - , ? , .

0
source

Source: https://habr.com/ru/post/1713426/


All Articles