Tutorials: Using the XmlHTTP Object

Introduction
In this tutorial, I'm going to write a quick little XmlHTTP function that pulls data out of the title tags in an html document, then displays the data in a MessageBox.

Basic Object Creation
When you first create a new DesktopX object, and go in and edit the script, you see this: 'Called when the script is executed Sub Object_OnScriptEnter

End Sub

'Called when the script is terminated Sub Object_OnScriptExit

End Sub We wont be needing that, so lets edit it a bit, so that it looks like this: Sub Object_OnScriptEnter

End Sub

Basic XmlHTTP Structure
First, lets add in our basic XmlHTTP code segment from here on the Wiki: Sub Object_OnScriptEnter address = "http://www.cerebrojd.net" Dim xmlhttp Set xmlhttp = CreateObject("Microsoft.XmlHttp") xmlhttp.Open "GET",address,False xmlhttp.send "" PageSource = xmlhttp.responseText End Sub Now we have the page's raw html in the PageSource variable! After dashing over to here, I learn that I can pull specific bits out of that data using the InStr function and the Mid function. Lets quickly discuss those, then move along into implementing them!


 * InStr: The InStr function returns the position of the first occurrence of one string within another.
 * InStr([start,]StringToSearch,StringToFind)


 * Mid: The Mid function returns a specified number of characters from a string.
 * Mid(string,start[,length])

Ok, now that thats over with, onward!

What To Do
Basically, we want to find the first instance of, then the first instance of. By subtracting the position of from, we figure out how long of a string we need to return. (This isnt exactly true, but its close. We need to subtract 7 from the length since its counting in it as well... we'll need to add 7 to the start point as well, but whatever.) Sub Object_OnScriptEnter address = "http://www.cerebrojd.net" Dim xmlhttp Set xmlhttp = CreateObject("Microsoft.XmlHttp") xmlhttp.Open "GET",address,False xmlhttp.send "" PageSource = xmlhttp.responseText

FirstNum = InStr(1,PageSource," ") + 7 SecondNum = InStr(1,PageSource," ") Length = SecondNum - FirstNum TheText = Mid(PageSource,FirstNum,Length)

MsgBox(TheText) End Sub Voila, done! A MessageBox should display when you apply the script with the title of my website in it! :-)