Archive for October, 2007
e4x webservices html
Have you've ever loaded some data in to Flex and tried to grab text from one of the nodes (html or xml ) but been unable to? Can you see the data being returned (via the debugger) but still can't extract the text from a node ... or even access the node itself?!
I won't go in to the reasons behind this, but last night I wanted to write a very small app to take a random word and append it to various other words. After 5 minutes of searching for a random-word WebService I got bored and decided to just grab the value from an html page I found:
-
<a href="http://wordie.org/random" title="Wordie.org random word page" target="_blank">http://wordie.org/random</a>
(this sometimes returns more than one word, but I didn't know that at the time)
The page has a single pair of h1 tags. This was a throwaway app so I had no problem with the idea of loading in the page content, grabbing the node I wanted, and tracing out the text. It was going to take me all of about 10 seconds to write the code and get it working. Yeah ... not quite that easy.
I could see the entire page being loaded in. I could trace out event.result, but when I tried to access anything else such as event.result..h1 I got a big fat 'nuthin' back (my results were being returned as e4x, so I knew that wasn't the issue).
To cut a long story short, if you look at the html source for the random word page you will see this:
-
<html xmlns="http://www.w3.org/1999/xhtml">
It has an xml namespace. If you want to use e4x to get at the node data you need to create a new namespace in your file and set it equal to the namespece value of the html:
-
private namespace webNameSpace = "http://www.w3.org/1999/xhtml";
-
use namespace webNameSpace;
I've put a mind-blowing example of this in action here (there may be a dramatic pause before the text displays )
Has your heartrate returned to normal yet? Okay, I'll continue... this app will run from FlexBuilder fine, but if you want to pop it up on a server then you will need to do one more thing so that you don't encounter player security issues (the data is coming from a different domain). To get it to work from your server, you will need to use a proxy. My hosting package includes php, and so I decided to use this ready-made proxy script.
(The above proxy requires the cURL library to be enabled - you can check this by uploading a phpinfo file to your server. Run it and check the output to see if cURL is enabled).
If you're curious to see the code for the random-word example, you'll find it below:
-
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml" creationcomplete="myService.send()">
-
<mx:script>
-
<!--[CDATA[
-
import mx.rpc.events.ResultEvent;
-
-
//WITHOUT THIS e4x DOESN'T ALWAYS WORK
-
private namespace webNameSpace = "http://www.w3.org/1999/xhtml";
-
use namespace webNameSpace;
-
-
private function handleResult(event:ResultEvent):void
-
{
-
/*If you didn't use the namespace, you would see the html in variable aaa
-
but would not be able to extract a particular node using e4x, and so
-
variable bbb would remain blank*/
-
var aaa:* = event.result;
-
var bbb:* = event.result..h1;
-
randomWordDisplay.text = "The random text is: " + event.result..h1.text();
-
}
-
]]-->
-
</mx:script>
-
-
<mx:httpservice id="myService" url="http://www.yoursite.com/proxy.php?url=http%3A//wordie.org/random" resultformat="e4x" result="handleResult(event)">
-
<mx:text id="randomWordDisplay"/>
-
</mx:httpservice>
-
</mx:application>
FlexBuilder (i.e. Eclipse) – ‘Go Into’
A while ago I posted a FlexBuilder/Eclipse work-flow tip about the working sets feature, which allows you to hide projects in your navigator pane. This is great if your projects comprise of multiple libraries, but there is another, quicker way to focus in on a single project, and that is to right-click the project and select 'Go Into' (or 'open in new window'). This will set the root-view of the Flex Navigator panel to be the root folder of your project and rid you of all that unnecessary visual clutter. To return back to the full list of projects, click the left-arrow at the top (as indicated in the graphic below):

2 comments
xml reference gotcha
I ran across some odd behaviour yesterday. I declared some dummy XML within a script block, looped over it and popped any nodes matching my query in to an XMLList.
That was all I did with those matching nodes ... pop them in an XMLList, but this was changing my original XML - fair enough, I can see this issue may be down to referencing, however rather than remove the node from my original XML as you may expect, it was duplicating the node.
I posted about the problem here. The post contains some copy & paste test code so you can try it for yourself. I'd be very interested in your feedback. As you can see, I had an idea that it was a reference problem, and therefore had a solution (the copy() method), but the real question is, should this be happening in the first place?
2 comments