0

I'm currently attempting to communicate with an API using Flex as the client. I'm using Flash Builders HTTPServices to do so, bit of a noob to all this. All works correctly if the accessed XML files elements are formatted correctly however if I add a hyphen to the element name I get flex warning me to the fact ie. The returned object contains an invalid name "created-on" that does not conform to the Actionscript identifier naming guidelines

This works:

<?xml version="1.0" encoding="UTF-8"?>
<projects type="array">
  <project>
    <createdon>2010-07-10</createdon>
    <name>Project 1</name>
  </project>
</projects>

This does not:

<?xml version="1.0" encoding="UTF-8"?>
<projects type="array">
  <project>
    <created-on>2010-07-10</created-on>
    <name>Project 1</name>
  </project>
</projects>

My question then is what can I do to make the returned xml actionscript friendly. Is there a standard or a best practice for this? I obviously don't have any control over the xml being passed.

Cheers

cammy
  • 37
  • 2

3 Answers3

0

If you really want to access the content without the '-' set the resultFormat to 'text' and then do something like:

onLoad(event:ResultEvent):void
{
     var result:String = event.result;
     result = result.replace(/created-on/g, 'createdOn');
     var xml:XML = new XML(result);
     // do your processing now...
}
dannrob
  • 1,061
  • 9
  • 10
0

For future reference, use square brackets. For example:

private var createdOn:String;

protected function myService_requestHandler(event:ResultHandler):void
{
    createdOn = event.result.projects.project["created-on"];
}
Pakman
  • 2,170
  • 3
  • 23
  • 41
-1

Set the resultFormat of HTTPService to e4x or xml. The default value is object and hence Flex tries to create ActionScript object for each elements in the xml tree.

<mx:HTTPService resultFormat="xml" other="attributes go here"/>
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • @Pakman I do not have Flex builder to try this, so hope you verified before down voting. – Amarghosh Feb 15 '12 at 19:20
  • The author asked how to handle XML nodes with hyphens. Even if you change the `resultFormat` to `e4x`, you still have to use brackets to reference the node. But, I'm new to Flex, so I could be wrong. – Pakman Feb 16 '12 at 15:54