3

Could someone explain why the FLEX 4.5 XMLDecoder does this to my XML-data?

var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>08.00</xmltag> );
// object = "08.00"

var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>11.00</xmltag> );
// Object = "11" (HEY! Where did my '.00' part of the string go?)

var decoder:XMLDecoder = new XMLDecoder;
var $object:Object = decoder.decode( <xmltag>11.30</xmltag> );
// Object = "11.3" (HEY! Where did my '0' part of the string go?)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Erik
  • 41
  • 3
  • looks like it is trying to type cast it. have you tried to wrap the data in a cData tag? – The_asMan May 31 '11 at 19:21
  • hm, looking closer at the debugger ... the first value (08.00) comes back as a string. But the other two (11 & 11.3) has been cast to a ”non” String. – Erik May 31 '11 at 19:25
  • Nope! Wrapping the string in a cData tag didn't help. – Erik May 31 '11 at 20:05

2 Answers2

1

So, to the answer the original question of why this is happening:

In the source code for SimpleXMLDecoder (which I'm guessing has similar functionality to XMLDecoder), there's a comment in the function simpleType():

//return the value as a string, a boolean or a number.
//numbers that start with 0 are left as strings
//bForceObject removed since we'll take care of converting to a String or Number object later

numbers that start with 0 are left as strings - I guess they thought of phone numbers but not decimals.

Also, because of some hacky implicit casting, you actually have three different types -

  • "0.800" : String
  • 11 : int
  • 11.3: Number
Kevin Li
  • 1,540
  • 2
  • 17
  • 23
1

The Flex deserializer also gave me issues with this. It may be interpreting them as Number objects and thus they will return short representations when toString() is called.

Try using .toFixed(2) whenever you need to print a value such as 11.00

var $object:Object = decoder.decode( <xmltag>11.00</xmltag> );
trace($object); //11
trace($object.toFixed(2)); //11.00
Ian T
  • 761
  • 4
  • 7
  • Sounds right. The bad part is that I’m not the one calling the XMLDecoder. I’m using Flash Builder to create valueObjects from an HTTPService returning xml. The decoder gets called somewhere in the ”result”-stack and my valueObjects comes back all messed up. – Erik May 31 '11 at 19:43
  • I use the toFixed() method when actually displaying the objects in the UI. This way things are displayed consistently in the application regardless of the internal representation values – Ian T May 31 '11 at 20:29
  • That gave me what I needed to go home with good conscience . And we’ll live to die with the decoder another day. Thanks a million! – Erik May 31 '11 at 20:50