PDA

View Full Version : XML navigation problem...


Jawnee
08-13-2008, 01:26 AM
Hey guys! I know nobody likes a newb who hasn't at least tried several different approaches before asking for help so I've invested a fair amount of time trying to make sense of this myself (using w3schools.com, the adobe help files, and my two flash books) prior to coming here with this question.

That said, I'm sure the solution is probably an obvious one. I can almost smell the solution like it's right under my nose but for whatever reason it continues to elude me. Anyhow, enough rambling... onto the problem at hand.

So basically I'm building my first flash site, and I'm pulling information from several different RSS feeds to help give this site some content. The first two feeds (from youtube and picasa) are working fine but I can't seem to get my blogger feed to work for some reason and I'm sure it's a simple node navigation problem.

For reference here is a drastically simplified version of the picasa feed (that works) and the actionscript to access one of the nodes of importance.

Picasa Feed
<?xml version='1.0' encoding='UTF-8'?>

<rss xmlns:atom='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
xmlns:exif='http://schemas.google.com/photos/exif/2007'
xmlns:geo='http://www.w3.org/2003/01/geo/wgs84_pos#'
xmlns:gml='http://www.opengis.net/gml'
xmlns:georss='http://www.georss.org/georss'
xmlns:photo='http://www.pheed.com/pheed/'
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:batch='http://schemas.google.com/gdata/batch'
xmlns:gphoto='http://schemas.google.com/photos/2007' version='2.0'>

<channel>
<item>
.
.
<title>Title of item</title>
.
.
</item>
<item>
.
.
.
</channel>
</rss>


Simple call to retrieve the title item above
_galleryXml is the XML object containing the above XML text
trace(_galleryXml.channel.item[0].title.toString());

Nothing to it right? Easy as pie! So what is baffling me is why I cannot navigate my blogger feed in the same way. Below is a simplified version of the blogger XML,(not as simple as the one above in case something I might skip is important) the code I'm trying to use, and error I'm receiving.

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'>
<id>tag:blogger.com,1999:blog-############</id>
<updated>2008-08-11T20:12:47.330-07:00</updated>
<title type='text'>###### #####</title>
<link rel='alternate' type='text/html' href='http://############.blogspot.com/'/>
<link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://############.blogspot.com/feeds/posts/default'/>
<link rel='self' type='application/atom+xml' href='http://############.blogspot.com/feeds/posts/default'/>
<author>
<name>###</name>
<uri>http://www.blogger.com/profile/##########</uri>
<email>noreply@blogger.com</email>
</author>
<generator version='7.00' uri='http://www.blogger.com'>Blogger</generator>
<openSearch:totalResults>5</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry>
<id>tag:blogger.com,1999:blog-#########.post-########</id>
<published>2008-08-11T19:46:00.001-07:00</published>
<updated>2008-08-11T20:12:47.350-07:00</updated>
<category scheme='http://www.blogger.com/atom/ns#' term='actionscript3.0'/>
<category scheme='http://www.blogger.com/atom/ns#' term='flash'/>
<category scheme='http://www.blogger.com/atom/ns#' term='travelingtroll'/>
<title type='text'>ActionScript 3.0, moving, and me!</title>
<content>.....</content>
<link rel='alternate' type='text/html' href='http://############.blogspot.com/2008/08/actionscript-30-moving-and-me.html' title='ActionScript 3.0, moving, and me!'/>
<link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=############&amp;postID=############' title='0 Comments'/>
<link rel='replies' type='application/atom+xml' href='http://############.blogspot.com/feeds/############/comments/default' title='Post Comments'/>
<link rel='self' type='application/atom+xml' href='http://############.blogspot.com/feeds/posts/default/##'/>
<link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/######/posts/default/######'/>
<author>
<name>###</name>
<uri>http://www.blogger.com/profile/#########</uri>
<email>noreply@blogger.com</email>
</author>
</entry>
<entry>
.
.
</entry>
</feed>


Attempted calls to retrieve the title item above
_blogXml is the XML object containing the above XML text
trace(_blogXml.entry[0].title.toString());

trace(_blogXml.entry[0].title.(@type = "text").toString());

Each attempt gives me a different error:
TypeError: Error #1010: A term is undefined and has no properties.
at BlogLoader/buildBlogList()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()

TypeError: Error #1010: A term is undefined and has no properties.
at BlogLoader/buildBlogList()
at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunctio n()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/flash.net:URLLoader::onComplete()

So as far as I can tell feed is the root node in this file, like rss was in the picasa feed. That said I assumed I didn't need to include feed in the trace call. Out of frustration I tried it anyways and it still failed. I noticed the openSearch namespace declaration at the root level as well but as far as I can tell the entry nodes are directly inside of the feed root, and do not require the use of this namespace. Any help anyone can offer would be awesome. I feel like I'm missing something that's either blatantly obvious or just outside of my limited understanding of XML.

lordofduct
08-13-2008, 01:58 AM
easy thing to miss actually... I didn't see it right away myself.

xmlns is a namespace reserved in xml for declaring namespaces. Heh... that's kind of annoying to say.

Anyways, you can't just outright say:
xmlns="blah blah blah"

you're supposed to say:
xmlns:attNameSpace="blah blah blah"

or in your case:
xmlns='http://www.w3.org/2005/Atom'
should be:
xmlns:atom='http://www.w3.org/2005/Atom'

as you can see your blogger XML is malformed because it says the prior. I don't know how to get around that personally... don't know a lot about XML myself. Maybe some one else can tell you. But it's that right there that is making the rest of it unreadable... I copied that into my own local xml file and removed the xmlns="blah blah" part in <feed> and it worked then.

From what I've read the xmlns is a method to avoid conflicts with different XML variants which use the same element names.

Jawnee
08-13-2008, 02:18 AM
Nice catch man! Hmmm... well I wonder if there's a way to get around it. I saved a copy of the feed from blogger, modified it as you suggested and it worked fine. I'll go ahead and contact blogger support to see if this is a known issue. In the meantime I'm open to suggestions on how to bypass this. I just started getting into XML with actionscript 3.0 so I'm still pretty new at it myself. Maybe someone could advise me on how to programatically edit that line of the feed after it's been downloaded into memory. (If that's possible)

lordofduct
08-13-2008, 03:00 AM
I guess you could convert the whole thing to a string with XML.toXMLString() , search it for "xmlns=" and transplant that with:
"xmlns:attr" + num + "="
where num is an incrementing value incase it finds more then one instance of "xmlns=".

Then convert that string back to an XML.

You might want to set the RegExp to ignore whitespace incase the string says "xmlns ="

Jawnee
08-13-2008, 03:02 AM
I guess you could convert the whole thing to a string with XML.toXMLString() , search it for "xmlns=" and transplant that with:
"xmlns:attr" + num + "="
where num is an incrementing value incase it finds more then one instance of "xmlns=".

Then convert that string back to an XML.

You might want to set the RegExp to ignore whitespace incase the string says "xmlns ="

Hey it's worth a shot! :D I'll give it a try! Thanks again!

lordofduct
08-13-2008, 03:18 AM
kinda like this:

var str:String = "blah turkey butt xmlns= duh hurt xmlns:haha forgot to be my xmlns= something from my xmlns=";
trace(str);

var reg:RegExp = new RegExp("xmlns=");
var i:int = -1;

while(str.search(reg) >= 0)
{
i++;
str = str.replace(reg, "xmlns:attr" + i + "=");
}

trace(str);

traces:

blah turkey butt xmlns= duh hurt xmlns:haha forgot to be my xmlns= something from my xmlns=
blah turkey butt xmlns:attr0= duh hurt xmlns:haha forgot to be my xmlns:attr1= something from my xmlns:attr2=

Jawnee
08-13-2008, 03:44 AM
Awesome dude! I was going about it the slow clumsy way... forgot all about how great regular expressions are! For any newbs coming to the table that ever experience this same problem the only thing left to do after making the corrections to the string is to convert that string back into an xml object. This is accomplished with a simple call.

//Only if the object wasn't already declared
var myXmlObject:XML;

myXmlObject = new XML(str);

Simple as that!