|
Displaying an XML document
XML is an exciting topic for developers out there
and in this example we will create an simple XML file using
notepad (or any other text editor for that matter) and then
display this in our ASP page .
First start your text editor and enter the following
, this will be your XML file .
<?xml version="1.0" encoding="ISO-8859-1"?>
<news>
<item>
<title>new domain name</title>
<link>http://www.beginnersphp.co.uk</link>
<description>we have a new domain name for our PHP site</description>
</item>
</news>
Save this as myscripting1.xml for the purpose of this example
.
Now we will look at the ASP page to retrieve these values ,
we want the title , link and the description .
<%
Dim objXML
'create an instance of the MSXML parser
Set objXML = Server.CreateObject("microsoft.XMLDOM")
'load up or XML file , this is stored in the same directory
as the script
'in this example
objXML.Load (Server.MapPath("myscripting1.xml"))
'if there are no errors
If objXML.parseError.errorcode = 0 Then
'strTitle is the value of the child node of the <item>
element
strTitle = objXML.documentElement.firstChild.firstChild.text
'use the nodeList object , to get the link and description as
strings . The index is
'zero based so <link> = 1 and description = 2.
strLink = objXML.documentElement.firstChild.childNodes(1).Text
strDescription = objXML.documentElement.firstChild.childNodes(2).Text
'print out our title
response.Write (strTitle & "<br>")
'print out the URL
Response.Write (strLink & "<br>")
'print the description
Response.Write (strDescription & "<br>")
'if there are errors then we print a message
Else
Response.Write ("There was an error")
End If
'clean up our resources
Set objXML = Nothing
%>
|