Wikia

Scratchpad

Tutorials: Using the XmlHTTP Object

217,794pages on
this wiki
Discuss this page3

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:

Dim address    ' Address to retrieve source code from
Dim xmlhttp    ' the xmlhttp object
Dim PageSource ' the full source of the page

Sub Object_OnScriptEnter
	address = "http://www.cerebrojd.net"
	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!

Minor VBScript Functions

  • 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!

Final Steps

Basically, we want to find the first instance of <title>, then the first instance of </title>. By subtracting the position of <title> from </title>, 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 <title> in it as well... we'll need to add 7 to the start point as well, but whatever.)

Dim address    ' Address to retrieve source code from
Dim xmlhttp    ' the xmlhttp object
Dim PageSource ' the full source of the page
Dim FirstNum   ' Left-hand side of the response string
Dim SecondNum  ' Right-hand side of the response string
Dim Length     ' The numerical length of the response string
Dim TheText    ' The response string

Sub Object_OnScriptEnter
	address = "http://www.cerebrojd.net"
	Set xmlhttp = CreateObject("Microsoft.XmlHttp")
	xmlhttp.Open "GET",address,False
	xmlhttp.send ""
	PageSource = xmlhttp.responseText

	FirstNum = InStr(1,PageSource,"<title>") + 7
	SecondNum = InStr(1,PageSource,"</title>")
	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! :-)

Comments/Suggestions

Is it possible to send take a web page.. strip out some part of the page (maybe a flash obj) or a DIV section, write a new web page, and send it to a Web Browser Object?

  • Yes, its definately possible! Technically, you dont even have to write it to an html file. You can just access the Document.InnerHTML property of the Web Browser Object. I think this object uses this idea a bit.
-- CerebroJD 20:42, 11 July 2006 (UTC)

Would it not be a good practice in these examples to declare all variables before we use them?

  • Yes, it would be. I'll try to work on that... I'm really not good at doing that normally. -- CerebroJD 19:48, 17 July 2006 (UTC)
  • Alright, properly declared the variables. I'll test to make sure it still works when I get home. CerebroJD 23:04, 22 August 2006 (UTC)

Around Wikia's network

Random wikia