我們在編寫ASP代碼的時候,大家都知道可以通過post或者get獲得form表單的數據,那麼我們如何直接獲得其他頁面上的數據呢?這就要借助xmlhttp協議了。xmlhttp是XMLdom技術的一部分。
下面的代碼就是一個很簡單的例子,我們利用XMLhttp技術,把http://www.codetoad.com/站點首頁的代碼以XML的形式完全獲取,並且在頁面中輸出。
<%
Dim objXMLHTTP, XML
Set xml = Server.CreateObject("Microsoft.XMLHTTP")
XML.Open "GET", "http://www.codetoad.com/", False
' Pull the data from the web page
XML.Send
Response.write "Here's the Html we now have in our XML object"
Response.write "<BR><BR><BR>"
Response.Write "<xmp>"
Response.Write XML.responseText
Response.Write "</xmp>"
Response.write "<BR><BR><BR>"
Response.write " Now here's how the page looks:<BR><BR>"
Response.Write XML.responseText
Set XML = Nothing
%>
下面是另一個實例
<%
dim objHTTP , objXML , objXSL
set objHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objHTTP.open "GET", "http://p.moreover.com/CGI-local/page?c=Pop%20music%20revIEws&o=XML", false
objHTTP.send
set objXML = objHTTP.responseXML
set objXSL=Server.CreateObject("microsoft.XMLdom")
objXSL.async=false
objXSL.load(Server.MapPath("style.xsl"))
if (objXSL.parseError.errorCode = 0) then
Response.Write(objXML.transformnode(objXSL))
else
Response.Write "Error: " & objXSL.parseError.reason & " URL:" & objXSL.url
end if
Set objHTTP = Nothing
Set objXML = Nothing
Set objXSL = Nothing
%>
style.xsl:
<xsl:stylesheet XMLns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<Html>
<head>
<TITLE>moreover...</TITLE>
</head>
<body BGCOLOR="ffffff">
<DIV ALIGN="center">
<TABLE BGCOLOR="ffffff" BORDER="0" CELLPADDING="4" CELLSPACING="0" WIDTH="100%">
<xsl:for-each select="moreovernews/article">
<TR VALIGN="middle">
<TD ALIGN="left" BGCOLOR="ffffff">
<xsl:attribute name="HREF">
<xsl:value-of select="url"/>
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<xsl:value-of select="headline_text"/>
<xsl:attribute name="HREF">
<xsl:value-of select="document_url"/>
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<xsl:value-of select="source"/>
<xsl:attribute name="HREF">
<xsl:value-of select="Access_registration"/>
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<xsl:value-of select="Access_status"/>
<xsl:value-of select="harvest_time"/> GMT
</TD>
</TR>
</xsl:for-each>
</TABLE>
</DIV>
</body>
</Html>
</xsl:template>
</xsl:stylesheet>