1 Reply Latest reply on Sep 10, 2007 5:52 PM by Senor_Roberto

    XML problem

    P Steven
      I am trying to convert a string to xml but for some reason when I convert to xml it only displays the "encoded string". See below the code snippet for what is traced. Any idea what I am doing wrong?

      Here is my test file

      var encodedString:String = "encoded string";

      var XMLString:String;
      XMLString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; XMLString += "<file>";
      XMLString += "<![CDATA[" + encoded string + "]]>";
      XMLString += "</file>";

      trace ("XMLString = " + XMLString); // Trace 1

      var dataToSend:XML = new XML(XMLString);

      trace ("dataToSend = " + dataToSend); // Trace 2

      var scriptPath:String = uploadUrl + "?xml=" + dataToSend + "&key=" + _key + "&session=" + _sessionId;

      -------------
      Trace Results
      -------------
      1.
      XMLString = <?xml version="1.0" encoding="UTF-8"?><file><![CDATA[encoded string]]></file> 2.
      encoded string

        • 1. Re: XML problem
          Senor_Roberto Level 1
          From the API docs:

          quote:

          * If the XML object has simple content, toString() returns the String contents of the XML object with the following stripped out: the start tag, attributes, namespace declarations, and end tag.

          * If the XML object has complex content, toString() returns an XML encoded String representing the entire XML object, including the start tag, attributes, namespace declarations, and end tag.

          To return the entire XML object every time, use toXMLString().

          Because your XML string is 'simple', it's being implicitly converted into a simple string. So change your line:

          var scriptPath:String = uploadUrl + "?xml=" + dataToSend + "&key=" + _key + "&session=" + _sessionId;

          To

          var scriptPath:String = uploadUrl + "?xml=" + dataToSend. toXMLString() + "&key=" + _key + "&session=" + _sessionId;

          Does that work?

          Cheers