[flexcoders] Re: How to extract data from XML
OMFG the namespace was the problem. After I added that line I was able to call Alert.show(myXML.logo.toXMLString()); and it worked like a charm. Thanks now I can finally start doing stuff with my application. This snag has been holding me up for a few days. One last thing, how would I go about extracting just what is between the tags logo and /logo instead of the entire element and attributes? Would I have to convert it to a single XML object first like you said? Or is there some function that can do that? Thanks for all your help Tracy, I'm starting to understand how XML works now because of you. -Leighton --- In flexcoders@yahoogroups.com, Tracy Spratt [EMAIL PROTECTED] wrote: Ok, you may have a namespace problem. They can be a PITA. I'm not an expert and you should reveiw the docs/google if this doesn't work but try putting this in the declarations section of your app/component(instance scope level): default xml namespace = http://www.w3.org/2005/Atom;; There is more stuff in the xmlns declaration in your sample xml: feed xmlns=http://www.w3.org/2005/Atom http://www.w3.org/2005/Atom ... I do not understand what that second part of the string is. You might have to include that as well in the default namespace declaration. You can avoid the namespace issue if you use the node index to get a node, but this is a very clunky way to do it. To see, try: Alert.show(myXML.children()[4].toXMLString()) XMLList always has 0-n xml nodes in it. (the result of an e4x expression is never null) If there is only one node, then many of the XML methods will work on it the same as with XML. But for clarity, if I know I want to work with a single XML node then I do: var myXMLList:XMLList = myXML.logo; var xmlLogo:XML = myXMLList[0]; If just you do this below, you will get a datatype error: var xmlLogo:XML = myXML.logo; //even if that expression returns only a single node. Tracy From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of wild.katana Sent: Thursday, May 22, 2008 7:08 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: How to extract data from XML Okay, first of all, thanks for all of your help, Tracy. I have checked the myXML variable with Alert.show(myXML) and it shows what I pasted in the first post (that's where I got it). So I know that myXML contains that. I tried doing Alert.show(myXML.logo.toXMLString()) but it is still showing nothing. I have read the documentation on using XML and tried many of the things it says, but to no avail. One thing I don't wuite understand... What is the difference between XML and XMLList? If the myXML.logo returns an XMLList, then should I make a variable that contains it first? Like var myXMLList:XMLList = myXML.logo; Alert.show(myXMLList); ? I will try that but let me know if that is what you were meaning or not... Thanks again.. --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Tracy Spratt tspratt@ wrote: First, did you trace or alert your XML in yur result handler? If you did not and are assuming you know what your xml looks like exactly, do that now. Second, when attempting to view XML, always use toXMLString() (search the archives for why): Alert.show(myXML.logo.toXMLString()) Third, when writing e4x expressions, unless you are very certain of yourself, it is best to do it one step at a time, using temporary XML or XMLList variables as needed, and tracing the contained value using toXMLString(). Note that all e4x expressions(myXML.logo is one) return XMLList, not XML. The docs will show how to handle this. Fourth, review the XML class in the language Ref and the working with data cha[ter in the developers guide for an explanation on how to work with XML. The link below is for Flex 2, but the XML stuff has not changed. http://livedocs.adobe.com/flex/2/docs/1910.html http://livedocs.adobe.com/flex/2/docs/1910.html Tracy From: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com [mailto:flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com ] On Behalf Of wild.katana Sent: Thursday, May 22, 2008 4:00 AM To: flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com Subject: [flexcoders] How to extract data from XML Hello, I have loaded an XML from a URL using URLLoader. Now that I have it in an object, how do I access individual parts of that data? The XML looks like this: feed xmlns=http://www.w3.org/2005/Atom http://www.w3.org/2005/Atom http://www.w3.org/2005/Atom http://www.w3.org/2005/Atom xmlns:openSearch=http://a9.com/-/spec/opensearchrss/1.0/ http://a9.com/-/spec/opensearchrss/1.0/ http://a9.com/-/spec/opensearchrss/1.0/ http://a9.com/-/spec/opensearchrss/1.0
[flexcoders] How to extract data from XML
Hello, I have loaded an XML from a URL using URLLoader. Now that I have it in an object, how do I access individual parts of that data? The XML looks like this: feed xmlns=http://www.w3.org/2005/Atom; xmlns:openSearch=http://a9.com/-/spec/opensearchrss/1.0/; xmlns:gml=http://www.opengis.net/gml; xmlns:georss=http://www.georss.org/georss; xmlns:media=http://search.yahoo.com/mrss/; xmlns:yt=http://gdata.youtube.com/schemas/2007; xmlns:gd=http://schemas.google.com/g/2005; idhttp://gdata.youtube.com/feeds/api/videos/id updated2008-05-22T07:54:46.868Z/updated category scheme=http://schemas.google.com/g/2005#kind; term=http://gdata.youtube.com/schemas/2007#video/ title type=textYouTube Videos matching query: football -soccer/title logohttp://www.youtube.com/img/pic_youtubelogo_123x63.gif/logo link rel=alternate type=text/html href=http://www.youtube.com/ link rel=http://schemas.google.com/g/2005#feed; type=application/atom+xml href=http://gdata.youtube.com/feeds/api/videos/ link rel=self type=application/atom+xml href=http://gdata.youtube.com/feeds/api/videos?start-index=11amp;max-results=10amp;orderby=publishedamp;vq=football+-soccer/ link rel=previous type=application/atom+xml href=http://gdata.youtube.com/feeds/api/videos?start-index=1amp;max-results=10amp;orderby=publishedamp;vq=football+-soccer/ link rel=next type=application/atom+xml href=http://gdata.youtube.com/feeds/api/videos?start-index=21amp;max-results=10amp;orderby=publishedamp;vq=football+-soccer/ author nameYouTube/name urihttp://www.youtube.com//uri /author ... So let's say I wanted to access the information in the logo element, how would I do this? My code looks like this: public var myLoader:URLLoader; //declare this outside of a function. The leading underscore is just a convention public var myXML:XML; //this too private function initUrlLoader():void { var XML_URL:String =http://gdata.youtube.com/feeds/api/videos?vq=football+-soccerorderby=publishedstart-index=11max-results=10;; var myXMLURL:URLRequest = new URLRequest(XML_URL); myLoader = new URLLoader(myXMLURL); myLoader.addEventListener(complete, xmlLoaded); } private function xmlLoaded(event:Event):void { myXML = XML(myLoader.data); trace(Data loaded.); // I want to pop up the information in the logo element Alert.show(myXML.logo); } The alert box isn't showing anything though... What am I doing wrong? Thanks
[flexcoders] Re: How do I use URLLoader?
Thank you, it worked. Now, could you tell me how to access the data from the myXML object? Say I wanted to display what's in the logo element in the XML file: ?xml version='1.0' encoding='UTF-8'? feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gml='http://www.opengis.net/gml' xmlns:georss='http://www.georss.org/georss' xmlns:media='http://search.yahoo.com/mrss/' xmlns:yt='http://gdata.youtube.com/schemas/2007' xmlns:gd='http://schemas.google.com/g/2005' idhttp://gdata.youtube.com/feeds/api/videos/idupdated2008-05-22T01:57:19.392Z/updatedcategory scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video'/ title type='text'YouTube Videos matching query: football -soccer /title logohttp://www.youtube.com/img/pic_youtubelogo_123x63.gif/logo Then how would I do this? Alert.show(myXML.feed.logo); Just returns as nothing... Thanks for your help Tracy! -Leighton --- In flexcoders@yahoogroups.com, Tracy Spratt [EMAIL PROTECTED] wrote: And IMPORTANT: do not initialize/set it when you declare it. You can only initialize instance variables to simple values. Set its value inside the function. public var _myLoader:URLLoader; //declare this outside of a function. The leading underscore is just a convention public var _myXML:XML; //this too ... private function initUrlLoader():void { var XML_URL:String =http://whatever http://whatever/ ; var myXMLURL:URLRequest = new URLRequest(XML_URL); _myLoader = new URLLoader(myXMLURL); _myLoader.addEventListener(complete, xmlLoaded); ... } function xmlLoaded(event:Event):void { _myXML = XML(_myLoader.data); trace(Data loaded.); } Tracy From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Tracy Spratt Sent: Wednesday, May 21, 2008 8:42 PM To: flexcoders@yahoogroups.com Subject: RE: [flexcoders] Re: How do I use URLLoader? Declare the variable in the instance scope, that is, not inside a function. That will make it available anywhere within the class instance. In non-OO terms, you might call it a global variable, but it is not really global, only global to the class instance, hence the term instance variable. Tracy From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of wild.katana Sent: Wednesday, May 21, 2008 4:53 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: How do I use URLLoader? Do you mean something like this: public var myLoader:URLLoader = new URLLoader(myXMLURL); I tried it but it didn't work... What do you mean by instance variable? --- In flexcoders@yahoogroups.com mailto:flexcoders%40yahoogroups.com , Tracy Spratt tspratt@ wrote: myLoader needs to be an instance variable instead of a local one. Maybe the sencond will go away when the xmlLoaded function can compile. Consider HTTPService instead, it is a bit simpler to use. Tracy
[flexcoders] Re: How to extract data from XML
Okay, first of all, thanks for all of your help, Tracy. I have checked the myXML variable with Alert.show(myXML) and it shows what I pasted in the first post (that's where I got it). So I know that myXML contains that. I tried doing Alert.show(myXML.logo.toXMLString()) but it is still showing nothing. I have read the documentation on using XML and tried many of the things it says, but to no avail. One thing I don't wuite understand... What is the difference between XML and XMLList? If the myXML.logo returns an XMLList, then should I make a variable that contains it first? Like var myXMLList:XMLList = myXML.logo; Alert.show(myXMLList); ? I will try that but let me know if that is what you were meaning or not... Thanks again.. --- In flexcoders@yahoogroups.com, Tracy Spratt [EMAIL PROTECTED] wrote: First, did you trace or alert your XML in yur result handler? If you did not and are assuming you know what your xml looks like exactly, do that now. Second, when attempting to view XML, always use toXMLString() (search the archives for why): Alert.show(myXML.logo.toXMLString()) Third, when writing e4x expressions, unless you are very certain of yourself, it is best to do it one step at a time, using temporary XML or XMLList variables as needed, and tracing the contained value using toXMLString(). Note that all e4x expressions(myXML.logo is one) return XMLList, not XML. The docs will show how to handle this. Fourth, review the XML class in the language Ref and the working with data cha[ter in the developers guide for an explanation on how to work with XML. The link below is for Flex 2, but the XML stuff has not changed. http://livedocs.adobe.com/flex/2/docs/1910.html Tracy From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of wild.katana Sent: Thursday, May 22, 2008 4:00 AM To: flexcoders@yahoogroups.com Subject: [flexcoders] How to extract data from XML Hello, I have loaded an XML from a URL using URLLoader. Now that I have it in an object, how do I access individual parts of that data? The XML looks like this: feed xmlns=http://www.w3.org/2005/Atom http://www.w3.org/2005/Atom xmlns:openSearch=http://a9.com/-/spec/opensearchrss/1.0/ http://a9.com/-/spec/opensearchrss/1.0/ xmlns:gml=http://www.opengis.net/gml http://www.opengis.net/gml xmlns:georss=http://www.georss.org/georss http://www.georss.org/georss xmlns:media=http://search.yahoo.com/mrss/ http://search.yahoo.com/mrss/ xmlns:yt=http://gdata.youtube.com/schemas/2007 http://gdata.youtube.com/schemas/2007 xmlns:gd=http://schemas.google.com/g/2005 http://schemas.google.com/g/2005 idhttp://gdata.youtube.com/feeds/api/videos http://gdata.youtube.com/feeds/api/videos /id updated2008-05-22T07:54:46.868Z/updated category scheme=http://schemas.google.com/g/2005#kind http://schemas.google.com/g/2005#kind term=http://gdata.youtube.com/schemas/2007#video http://gdata.youtube.com/schemas/2007#video / title type=textYouTube Videos matching query: football -soccer/title logohttp://www.youtube.com/img/pic_youtubelogo_123x63.gif http://www.youtube.com/img/pic_youtubelogo_123x63.gif /logo link rel=alternate type=text/html href=http://www.youtube.com http://www.youtube.com / link rel=http://schemas.google.com/g/2005#feed http://schemas.google.com/g/2005#feed type=application/atom+xml href=http://gdata.youtube.com/feeds/api/videos http://gdata.youtube.com/feeds/api/videos / link rel=self type=application/atom+xml href=http://gdata.youtube.com/feeds/api/videos?start-index=11amp;max-r esults=10amp;orderby=publishedamp;vq=football+-soccer http://gdata.youtube.com/feeds/api/videos?start-index=11amp;max-result s=10amp;orderby=publishedamp;vq=football+-soccer / link rel=previous type=application/atom+xml href=http://gdata.youtube.com/feeds/api/videos?start-index=1amp;max-re sults=10amp;orderby=publishedamp;vq=football+-soccer http://gdata.youtube.com/feeds/api/videos?start-index=1amp;max-results =10amp;orderby=publishedamp;vq=football+-soccer / link rel=next type=application/atom+xml href=http://gdata.youtube.com/feeds/api/videos?start-index=21amp;max-r esults=10amp;orderby=publishedamp;vq=football+-soccer http://gdata.youtube.com/feeds/api/videos?start-index=21amp;max-result s=10amp;orderby=publishedamp;vq=football+-soccer / author nameYouTube/name urihttp://www.youtube.com/ http://www.youtube.com/ /uri /author ... So let's say I wanted to access the information in the logo element, how would I do this? My code looks like this: public var myLoader:URLLoader; //declare this outside of a function. The leading underscore is just a convention public var myXML:XML; //this too private function initUrlLoader():void { var XML_URL:String =http://gdata.youtube.com/feeds/api/videos?vq=football+-soccerorderby= publishedstart-index=11max-results=10 http://gdata.youtube.com/feeds/api
[flexcoders] How do I use URLLoader?
I have this code: var myXML:XML = new XML(); var XML_URL:String = http://gdata.youtube.com/feeds/api/videos?vq=football+-soccerorderby=publishedstart-index=11max-results=10;; var myXMLURL:URLRequest = new URLRequest(XML_URL); var myLoader:URLLoader = new URLLoader(myXMLURL); myLoader.addEventListener(complete, xmlLoaded); function xmlLoaded(event:Event):void { myXML = XML(myLoader.data); trace(Data loaded.); } And I want to read the XML file into a XML object in Flex, so that I can access different elements of it easier. But it is throwing up two errors when I try to compile it: 1120: Access of undefined property myLoader. 1120: Access of undefined property xmlLoaded. What am I doing wrong? Do I need to import any special things first? I am still pretty new to Flex...
[flexcoders] Re: How do I use URLLoader?
Hmm, I tried that but it didn't work... I previously tried these as well but they didn't do it, nor did the import flash.net.* ... hmmm import flash.net.URLLoader; import flash.net.URLRequest;
[flexcoders] Re: How do I use URLLoader?
Wierd, I can't post anything... Tracy, what do you mean by instance variable? I tried: public var myLoader:URLLoader = new URLLoader(myXMLURL); But it didn't work.. Any ideas?
[flexcoders] Re: How do I use URLLoader?
Tracy, what do you mean about the instance variable? I tried this but it didn't work: public var myLoader:URLLoader = new URLLoader(myXMLURL); Any ideas?
[flexcoders] Re: How do I use URLLoader?
Wierd, I can't post anything... Tracy, what do you mean by instance variable? I tried: public var myLoader:URLLoader = new URLLoader(myXMLURL); But it didn't work.. Any ideas?
[flexcoders] URLLoader
Wierd, I can't post any replies on my old topic... Tracy, what do you mean by instance variable? I tried: public var myLoader:URLLoader = new URLLoader(myXMLURL); But it didn't work.. Any ideas?
[flexcoders] Re: How do I use URLLoader?
Wierd, I can't post anything... Tracy, what do you mean by instance variable? I tried: public var myLoader:URLLoader = new URLLoader(myXMLURL); But it didn't work.. Any ideas? --- In flexcoders@yahoogroups.com, Tracy Spratt [EMAIL PROTECTED] wrote: myLoader needs to be an instance variable instead of a local one. Maybe the sencond will go away when the xmlLoaded function can compile. Consider HTTPService instead, it is a bit simpler to use. Tracy From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Charlie Griefer Sent: Wednesday, May 21, 2008 4:44 PM To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] How do I use URLLoader? http://livedocs.adobe.com/flex/3/langref/flash/net/URLLoader.html http://livedocs.adobe.com/flex/3/langref/flash/net/URLLoader.html import flash.net.*; I'm pretty new to Flex as well, but having the docs bookmarked has come in very handy on a number of ocassions so far :) http://livedocs.adobe.com/flex/3/langref/index.html http://livedocs.adobe.com/flex/3/langref/index.html On Wed, May 21, 2008 at 1:37 PM, wild.katana [EMAIL PROTECTED] mailto:wild.katana%40gmail.com wrote: I have this code: var myXML:XML = new XML(); var XML_URL:String = http://gdata.youtube.com/feeds/api/videos?vq=football+-soccerorderby=p ublishedstart-index=11max-results=10 http://gdata.youtube.com/feeds/api/videos?vq=football+-soccerorderby=p ublishedstart-index=11max-results=10 ; var myXMLURL:URLRequest = new URLRequest(XML_URL); var myLoader:URLLoader = new URLLoader(myXMLURL); myLoader.addEventListener(complete, xmlLoaded); function xmlLoaded(event:Event):void { myXML = XML(myLoader.data); trace(Data loaded.); } And I want to read the XML file into a XML object in Flex, so that I can access different elements of it easier. But it is throwing up two errors when I try to compile it: 1120: Access of undefined property myLoader. 1120: Access of undefined property xmlLoaded. What am I doing wrong? Do I need to import any special things first? I am still pretty new to Flex... -- A byte walks into a bar and orders a pint. Bartender asks him What's wrong? Byte says Parity error. Bartender nods and says Yeah, I thought you looked a bit off.
[flexcoders] Re: How do I use URLLoader?
This still isnt working. I tried import flash.net.* and a few other things, no go.. Any other ideas? Tracy, can you clarify what you mean? --- In flexcoders@yahoogroups.com, wild.katana [EMAIL PROTECTED] wrote: I have this code: var myXML:XML = new XML(); var XML_URL:String = http://gdata.youtube.com/feeds/api/videos?vq=football+-soccerorderby=publishedstart-index=11max-results=10;; var myXMLURL:URLRequest = new URLRequest(XML_URL); var myLoader:URLLoader = new URLLoader(myXMLURL); myLoader.addEventListener(complete, xmlLoaded); function xmlLoaded(event:Event):void { myXML = XML(myLoader.data); trace(Data loaded.); } And I want to read the XML file into a XML object in Flex, so that I can access different elements of it easier. But it is throwing up two errors when I try to compile it: 1120: Access of undefined property myLoader. 1120: Access of undefined property xmlLoaded. What am I doing wrong? Do I need to import any special things first? I am still pretty new to Flex...
[flexcoders] Re: How do I use URLLoader?
Hmmm, do you mean like this: public var myLoader:URLLoader = new URLLoader(myXMLURL); I tried that but it didn't work. What do you mean by instance variable? --- In flexcoders@yahoogroups.com, Tracy Spratt [EMAIL PROTECTED] wrote: myLoader needs to be an instance variable instead of a local one. Maybe the sencond will go away when the xmlLoaded function can compile. Consider HTTPService instead, it is a bit simpler to use. Tracy
[flexcoders] Re: How do I use URLLoader?
Do you mean something like this: public var myLoader:URLLoader = new URLLoader(myXMLURL); I tried it but it didn't work... What do you mean by instance variable? --- In flexcoders@yahoogroups.com, Tracy Spratt [EMAIL PROTECTED] wrote: myLoader needs to be an instance variable instead of a local one. Maybe the sencond will go away when the xmlLoaded function can compile. Consider HTTPService instead, it is a bit simpler to use. Tracy