2 Replies Latest reply on Feb 14, 2007 5:50 AM by cgallemore23

    Parsing XML

    cgallemore23
      Hi, I am new to ActionScript and I ran into a problem the other day trying to parse XML. I'm writing a flex application and using Flex Builder to write and compile my code. Say I have the following xml:

      <update>
      <registration>
      <service-name>

      In my code I do the following:

      var xml:XML = XML (event.result);
      var list:XMLList = xml..registration;

      I next need to loop over all of the registration elements and pull out the value of service-name. This is where I have a problem. So for example when I say:

      var serviceName:String = list.service-name;

      I get a compile error saying "Implicit coercion of a value of type Number to an unrelated type String"

      I got around this by changing the xml to use camel casing instead of a dash (<serviceName> instead of <service-name>).

      So my question is, is it not well formed xml to use a dash within a tag name (<service-name>), or is this a bug with flex.
        • 1. Re: Parsing XML
          _Albertx_ Level 1
          Hi...

          Actually, it's not a bug in Flex, the problem here is that ActionScript reads the xml and use DOM (Document Object Model) for being able to access the xml tags.

          DOM is based in converting all of your tags in identifiers into your ActionScript XML object. So they are converted to variables (well, not exactly variables but identifiers), and because you can not have dashes in variable names you get that error. You can only have alphabetic characters, numbers, _ and $.

          Maybe it's is possible to get the value and not to get the error if you type

          xml["service-name"] instead of xml.service-name

          But actually, it is not well formed xml if it has dashes in the tag names.
          • 2. Re: Parsing XML
            cgallemore23 Level 1
            Thanks for the quick response, that makes perfect sense.