The VBScript Network and Systems Administrator's Cafe

Mar 14 2008   5:57PM GMT

Using XML in vbscript via Microsoft.XMLDOM to work with data feeds



Posted by: Jerry Lees
Tags:
360voice
DataManagement
Microsoft.XMLDOM
VBScript
XML

In this installment I wanted to both celebrate and congratulate the winners of the ITKE Challenge that has been running over the last few months for an XBOX 360 ELITE system, among other prizes. I also wanted to write about XML use in vbscript as well so the challenge was how to accomplish both tasks…

Then I remembered a great community site, that I’ve been a member of for a little over a year or so, called 360voice. Basically, those guys have a truly awesome idea over there… I play games with my Xbox 360 and it blogs about the times we have—and I don’t have to do any work on my own. ;-) If you have a 360 and don’t have an account setup, go over and check it out, tell ‘em Jlees sent you. Heck, while you are at it watch my blog there and drop me a note.

At any rate, one of my challenges was to get everyone the same XML file, while not creating it myself because I wanted the script to be useful under certain circumstances. 360voice came through with their API Documentation! Think about it… now your scripting life has insite into your gaming life (and other’s as well) you could write a script to compare your achievements to your buddies, show the games you haven’t completed, and all sorts of other stuff. Why?? Because you can!

Consider this working code (that indecently pulls the XML directly from the website without saving it locally) that shows the last 10 days of gamerscore history for two gamertags in the output, in my case I chose mine, Jlees, and one of the owners of the site, changeagent, to do the comparison.. From this script alone you could graph your gamerscore and a buddies so you can compare and have irrefutable bragging rights about your gaming powers! (notice: that I put just about everything together we’ve talked about up to now in this one script, if your just joining us you might want to go back and brush up the last few weeks worth of blogs really quick. Also, I’ve noticed some cases where the sourcecode does not copy correctly, so I have uploaded the source to this article to my website here for you to download.)

Option Explicit

Dim GamerTag1,GamerTag2, GamerScore, Days, xmlDoc, x, y, Value, Count

GamerTag1 = “CrashSerious” ‘change this to your gamertag
GamerTag2 = “ChangeAgent” ‘change this to your buddies gamer tag
‘ a quick note on the following line. It’s great the folks at
www.360voice.com gave us this API
‘ While you likely can change this to something other than 10 below, I strongly encourage you To
‘ limit it to less than a month. The pulling of this data will no doubt have an impact on their servers
‘ and excessive pulls will likely get you blocked, the API removed, or restricted.
‘ Basically, be a good neighbor.
Days = 10 ‘ change this to the number of days to pull.

DisplayTagHistory(GamerTag1)
DisplayTagHistory(GamerTag2)

Sub DisplayTagHistory(GamerTag)
 set xmlDoc=CreateObject(“Microsoft.XMLDOM”)

 ReDim GamerScore(Days)

 xmlDoc.async=”false”
 xmlDoc.load(“
http://www.360voice.com/api/score-getlist.asp?tag=” & GamerTag & “&num=” & days)
 for Each x in xmlDoc.documentElement.childNodes
   If x.nodename = “info” Then ‘ this is where the gamer tag is located in the node named “gamertag”
    WScript.Echo GetTagValue(x,”gamertag”)
   End If
 
   If x.NodeName = “gamerscore” Then
  Count = 0
  For Each y In x.childnodes  ’ this is where the score for that day is located
         ’ in a node named “value”
   GamerScore(Count) = GetTagValue(y,”value”)
   ’below we output the data. You could easily change the “vbTab & “:” & vbTab” to
   ’a , and create a CSV or write to a file. the world’s wide open from here.
   WScript.Echo Date-Count & vbTab & “:” & vbTab & Gamertag & vbTab & “: ” & GamerScore(Count)
   Count = Count + 1
  Next
   End If
 Next
End sub

‘note this function assumes only one node named the same as SrchStr is in the XMLTAG child node.
‘ if there is more than one, only the last value will be returned.
Function GetTagValue(XMLTag, SrchStr)
 for each Value In XMLTag.childNodes
    if ucase(Value.nodename) = Ucase(SrchStr) Then
     GetTagValue = Value.text ‘get the value in the node requested.
    End If
 Next 
End Function

The output, with the script untouched, should look like so:

 JLees
3/14/2008 : JLees : 23740
3/13/2008 : JLees : 23730
3/12/2008 : JLees : 23730
3/11/2008 : JLees : 23730
3/10/2008 : JLees : 23730
3/9/2008 : JLees : 23730
3/8/2008 : JLees : 23700
3/7/2008 : JLees : 23700
3/6/2008 : JLees : 23700
3/5/2008 : JLees : 23700
ChangeAgent
3/14/2008 : ChangeAgent : 12817
3/13/2008 : ChangeAgent : 12817
3/12/2008 : ChangeAgent : 12817
3/11/2008 : ChangeAgent : 12817
3/10/2008 : ChangeAgent : 12817
3/9/2008 : ChangeAgent : 12817
3/8/2008 : ChangeAgent : 12817
3/7/2008 : ChangeAgent : 12817
3/6/2008 : ChangeAgent : 12817
3/5/2008 : ChangeAgent : 12777

I only used ONE new thing in this code Microsoft.XMLDOM, which we’ll discuss in a later entry but I wanted to call your attention to the following line in this entry:

xmlDoc.load(“http://www.360voice.com/api/score-getlist.asp?tag=” & GamerTag & “&num=” & days) 

 

This line is the one where you would specify the path for the XML file, whether it is a local file or a file on the internet—like this rss feed to my blog. Also note that the DisplayTagHistory subroutine is highly focused to this XML feed and will likely need some work to get it to display another.

Enjoy!

Extra Credit: Can you modify the code to include the other owner of the 360voice site and compare his score with mine and changeagent’s? His Gamertag is Fatty Chubs.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: