4 Replies Latest reply on Mar 27, 2007 6:17 AM by odyssey27

    How to convert an Object to XML definition

    David_S_deB Level 1
      Hi all,

      A quick question I want to convert an instance of an object (including it's properties) to XML. I have read the doco on SimpleXMLEncoder whixh states that : "The SimpleXMLEncoder class take ActionScript Objects and encodes them to XML. Use default serialization. "

      Will this class do the job? if so the example code does not really demonstrate it clearly that it will. And I haven´t been able to get it to work.

      If this is not the Class to use is there another or do I have to write my own class?

      Thank you in advance.

      David
        • 1. Re: How to convert an Object to XML definition
          FlightGuy Level 1
          SimpleXMLEncoder will do the job. Try something like this:

          var myEncoder:SimpleXMLEncoder = new SimpleXMLEncoder();
          var myXML:XMLNode = myEncoder.encodeValue(myObject);
          trace (myXML.toString());

          This will encode the object "myObject" as an XMLNode. Regrettably it produces an XMLNode, and not and XML. You can convert by doing this:

          var myRealXML:XML = new XML(myXML.toString());

          Tim
          • 2. Re: How to convert an Object to XML definition
            David_S_deB Level 1
            Hi Tim,
            Thanks for the quick reply. I tried your code above and the compiler tells me I was missing 1 Argument for (an instance of XML Docuement it looks like):
            var myEncoder:SimpleXMLEncoder = new SimpleXMLEncoder();
            And 2 Arguments for :
            var myXML:XMLNode = myEncoder.encodeValue(myobject);

            I ended up creating this missing parameters as follows and got the result I was looking for. Does this look right to you?

            var rootXML:XMLNode = new XMLNode(XMLNodeType.DOCUMENT_TYPE_NODE,"Root");
            var myQname:QName = new QName("namspace","local");
            var myXMLDocument:XMLDocument = new XMLDocument();
            var myEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(myXMLDocument);
            var myXML:XMLNode = myEncoder.encodeValue(objectToEncode,myQname,rootXML);
            trace (myXML.toString());
            var myRealXML:XML = new XML(myXML.toString());
            trace ("------------");
            trace (myRealXML.toXMLString());

            David
            • 3. Re: How to convert an Object to XML definition
              odyssey27 Level 1
              David,
              Just used your code to pass a converted object to a .net web service and it worked like a champ. I was down the same path but got hung up on the QName object.

              Thanks a million!
              Greg
              • 4. Re: How to convert an Object to XML definition
                odyssey27 Level 1
                David,
                Just used your code to pass a converted object to a .net web service and it worked like champ. I was down the same path but got hung up on the QName object.

                Thanks a million!
                Greg