Lets start from the beginning by explaining what is involved in using XML in our pages.
It basically consists of two main elements
Ok so how do we create these elements. Simple
To create the XML DOM, which is basically the holding point for your XML data you do the following
Dim XMLDOM
Set XMLDOM = Server.CreateObject("Msxml2.DOMDocument.3.0")
Ok so now that we have a place to put our XML now we need to put the data into it.
XMLDOM.load("yourxmlfile.xml")
So what have we just done, well we have created an XML DOM object to loaded a data island into it. This means that our variable XMLDOM now holds our XML data in its memory space. But what use is that if we cant see it, simple it means we can do what ever we like to get the information out of our XML file into our page.
For Example - If we are looking for a particular node (say the first node called item)
Set GetRootNode = XMLDOM.documentElement
Set FirstNodeText = GetRootNode.selectSingleNode("//item[0]/*").text
What we have done here is simple, the documentElement method pulls the parent node from the XML file, then the selectSingleNode allows us to pick any node we wish to or you can simply loop through all the nodes by using a for loop with the length of your nodes.
Load XML into Page and read First Contents
So to load an XML file and pull out the node value.
Firstly Create an XML file called ourxmlfile.xml as follows
<?xml version="1.0"?>
<fields>
<item>
<text>Text of first Item Node.</text>
</item>
<item>
<text>
Text of second Node</text>
</item>
</fields>
Then create an ASP page in the same directoy and enter the following code.
Dim XmlDOMDoc, GetRootNode
Set XmlDOMDoc = Server.CreateObject("Msxml2.DOMDocument.3.0")XmlDOMDoc.load(Server.MapPath("ourxmlfile.xml"))
Set GetRootNode = XmlDOMDoc.documentElement
response.write GetRootNode.selectSingleNode("//item[0]/*").text
When you run this page you should see the value of the first node in your browser.
Send XML / Form Data using XMLHttp
So what if we want to send XML to this page?
Again create the necessary page objects as follows
Dim XmlHttp, ResponseXml
Set XmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
NOTE - this time we ate creating an XMLHTTP oibject not the XMLDom
Then open the HTTP using the XML and POST your data as follows
XmlHttp.Open "POST", "http://someurl.com/yourpage", false
Once we have opened the pipe to send the data we just need to set the type of data we are send and send it on its way
XmlHttp.SetRequestHeader "Content-Type", "application/xml; charset=UTF-8"
XmlHttp.Send SomeDatatoSend
Note - this will send the data as XML data if you just wish to post your data as name/value pairs, just like your would when submitting a form, just change the lines above to the following
XmlHttp.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
XmlHttp.Send SomeDatatoSend
Note SomeDatatoSend is just a variable contained what you wish to send, it can be anything you like.
for example - SomeDatatoSend = "
Once we have sent it we need to listen for the response, either in the response text
XmlHttp.ResponseText
Note - when checking this you will get full response text back
or in the server status returned
XmlHttp.status
Note - when checking for this you will get the normal HTTP response codes back, i.e 200 is OK
So the complete code to send your data looks like
Dim XmlHttp, ResponseXML
Set XmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0")
XmlHttp.Open "POST", "http://someurl.com/yourpage", false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
XmlHttp.Send SomeDataToSend
Set ResponseXML= XmlHttp.ResponseText or XmlHttp.status
So now you are able to send data using XML and parse XML which is either posted to a page or loaded as a data island, your options are endless.
Happy XMLing
Thanks
Sean J Connolly
Visit AJAX Web Development Store
DMS - Document Management, Webmail
BuzzProperties.co.uk - Online Property Sales and Letting
No comments:
Post a Comment