Sign in to follow this  
Followers 0
Foxhound

XMLDomWrapper + YouTube GData Madness!

4 posts in this topic

#1 ·  Posted (edited)

Okay, so I was working on my YouTube API UDF when I decided to use the to make my life a bit easier.

Now , I may not be proficient with XPath but I've tried several times to get some simple data data off of the YouTube GData API but it won't work (returns -1, @error is 0 though)

I've tried _INetGetSource(), BinaryToString(InetRead()), InetGet(), and they all seem to return the XML data just fine.

I don't want to have to manually parse it using the string functions as there is a lot of data I need to pull.

Anyway, here's a test script I made to illustrate my dillema:

#include "_XMLDomWrapper.au3"
#include "Array.au3"
#include "INet.au3"
$videoid = 'YgFyi74DVjc'
Local $ytAPIURL = 'http://gdata.youtube.com/feeds/api/videos/' & $videoid & '?v=2'
InetGet($ytAPIURL,"output.xml")
_XMLFileOpen("output.xml") ;returns 1, success
$value = _XMLGetValue('/entry/id') ;returns -1, wtf
if IsArray($value) Then _ArrayDisplay($value)
trace($value)

Func trace($data="")
    ConsoleWrite(@extended&" "&@error&"  "&$value&@CRLF)
EndFunc

And here's a short sample of the XML structure for the YouTube API:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:app='http://www.w3.org/2007/app' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007' gd:etag='W/&quot;DE8GSX47eCp7ImA9WhZRFUk.&quot;'>
<id>tag:youtube.com,2008:video:YgFyi74DVjc</id>
<published>2010-08-13T13:46:28.000Z</published>
<updated>2011-04-11T18:33:48.000Z</updated><app:control>
<yt:state name='restricted' reasonCode='limitedSyndication'>Syndication of this video was restricted by the content owner.</yt:state>
</app:control>
<!-- .... ~~~  -->
</entry>

Can anyone point me in the right direction? I'm about to pull my hair out...

Edited by Foxhound

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Real quick look. Removing

'http://www.w3.org/2005/Atom'

fixes it.

Edited by notta

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

notta is right, there is a namespace problem. Your XPath of /entry/id says to look for an element /entry/id which has no namespace, but there is only /entry/id under the namespace xmlns.

So you have the specify the default namespace when you use _XMLFileOpen (the UDF facilitates this already). Unfortunately, using xmlns=namespace directly does not work because the UDF can not handle namespaces without a node prefix (it needs to use a NamespaceManager which is not implemented).

As an alternative to expanding the UDF, we can just use any unused node prefix such as "a" in the example, and use that in our XPath instead.

#include "_XMLDomWrapper.au3"
#include "Array.au3"
#include "INet.au3"
$videoid = 'YgFyi74DVjc'
Local $ytAPIURL = 'http://gdata.youtube.com/feeds/api/videos/' & $videoid & '?v=2'
InetGet($ytAPIURL,"output.xml")
$a = _XMLFileOpen("output.xml", "xmlns:a='http://www.w3.org/2005/Atom'") ;returns 1, success
trace($a)
$value = _XMLGetValue('/a:entry/a:id') ; returns "", wtf
if IsArray($value) Then _ArrayDisplay($value)
trace($value)

Func trace($data="")
    ConsoleWrite(@extended&" "&@error&"  "&$data&@CRLF)
EndFunc

For further reading:

XML Namespaces and How They Affect XPath and XSLT

How To Specify Namespace when Querying the DOM with XPath

In this case, using the qualified name is straightforward. When the default namespace is used, however, using the qualified name can be more difficult, as in the following example:

Note that no prefix is used in the node tags. The qualified name must still be used inside the XPath query, otherwise the query (for example, /Books/Book) returns no result because there are no matching nodes.

Edited by Manadar

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

XML...namespaces...interesting...I learned something new :unsure:

I believe I'm getting the hang of it.

So let's say if I want to pull some data from this same API with this structure.

<media:group>
   <media:description></media:description>
</media:group>

I would have to append

xmlns:media='http://search.yahoo.com/mrss/' or its respective schema to _XMLFileOpen() and

use 'a:entry/media:group/media:description' to pull data, am I correct?

I think I have a pretty solid idea of what you're talking about now. Thank you for your help.

Edited by Foxhound

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0