Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Friday, February 13, 2009

Namespaces are a pain

Friday, February 13, 2009   

var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener( Event.COMPLETE, onLoadXML );
myLoader.load( new URLRequest( "http://www.xignite.com/xquotes.asmx/GetQuote?Symbol=AAPL" ));
function onLoadXML( e:Event ):void
{
var ns:Namespace = new Namespace("http://www.xignite.com/services/");
var myXML:XML = new XML( e.target.data );

trace( "Name " + myXML.ns::Name );
trace( "Last " + myXML.ns::Quote.ns::Last );
trace( "Price Earnings " + myXML.ns::Statistics.ns::Price_Earnings );
trace( myXML.ns::News.ns::StockNews.ns::Headline[0] );
var len:Number = myXML.ns::News.ns::StockNews.ns::Headline.length();
trace( myXML.ns::News.ns::StockNews.ns::Headline[len-1]);
}
I mean, come on, that's just weird. Why couldn't I set the namespace once somehow and just use regular dot notation from then on out? (Unless I can and I am missing something). I pulled the code above out of my backside and lo' and behold it worked.

Namespaces are something I need to properly wrap my head around, as it seems it's coming up more and more. I wasn't even quite sure that my original parsing problem was namespace related or not. In the above XML returned, do they really need to define namespaces?

Here is something better (this is all still a bit of a pain):
var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener( Event.COMPLETE, onLoadXML );
myLoader.load( new URLRequest("http://www.xignite.com/xquotes.asmx/GetQuote?Symbol=AAPL" ));
myLoader.dataFormat = "XML";

function onLoadXML( e:Event ):void {
var myXML:XML = new XML(e.target.data);
if( myXML.namespace() != null)
default xml namespace = myXML.namespace();
trace("Outcome: ", myXML.Outcome);
trace("Delay: ", myXML.Delay);
}

Labels: , ,

 
 Return to the main page
Comments:

There are currently 1 Comments:

Blogger Beasta said...
“Think its best to create a file like 'org/mynamespaces/xignite.as'.

package org.mynamespace
{
public namespace xignite="http://www.xignite.com/services/"
}

import org.mynamespace.xignite;

Then in top of your parser class:
public class MyParser {
use namespace xignite;
[... code ... ]
}

Should enable you to use simple dot access.”
 
 Leave a comment