我的经典ASP代码连接到网址并显示XML,我的代码如下所示
Response.ContentType = "text/xml"
myRSSfile = "http://abc.com"
Set getPage = Server.CreateObject("Microsoft.XMLHTTP" )
getPage.Open "GET", myRSSfile, false
getPage.SetRequestHeader "Content-type", "text/xml"
getPage.Send
Response.Write(getPage.responseText)
'response.write getPage.Status
Set getPage = Nothing
XML如下
<userContent xmlns="http://www.abc.com/userContent" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.abc.com/abc.xsd">
<questions>
<question>
<item>
<sku>SCG20056-006</sku>
<title>Test me machine</title>
</item>
<text>
We are replacing a built in machine how it would be ?
</text>
<dateTime>2011-11-10T22:43:02Z</dateTime>
<answer>
<user>
<firstName>Raj</firstName>
<lastName>lastname</lastName>
</user>
<text>
We have been very happpy with the replacement
</text>
<dateTime>2011-11-21T21:00:24Z</dateTime>
</answer>
<answer>
<user>
<firstName>john</firstName>
<lastName>wright</lastName>
</user>
<text>
not so happy
</text>
<dateTime>2011-11-21T21:00:24Z</dateTime>
</answer>
</question>
</questions>
<comments/>
</userContent>
我需要做的是显示
1)“问题/问题/文本”标签
2)显示该标签的所有问题的答案
“questions / question / answer / user / firstName”标签
&安培;
3)“问题/问题/答案/文本”标签
在Classic ASP中可以做到吗?
答案 0 :(得分:2)
采取这种方法的方法是使用XSL对收到的XML执行转换,以生成您想要显示的HTML。这是一个让您入门的示例:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:uc="http://www.abc.com/userContent" exclude-result-prefixes="uc">
<xsl:output method="html" />
<xsl:template match="/uc:userContent">
<div class="container">
<xsl:apply-templates select="uc:questions/uc:question" />
</div>
</xsl:template>
<xsl:template match="uc:question">
<b>Shopper asked:</b> <xsl:value-of select="uc:text" />
<ul>
<xsl:apply-templates select="uc:answer" />
</ul>
</xsl:template>
<xsl:template match="uc:answer">
<li>
<b>
<xsl:value-of select="uc:user/uc:firstName" />
<xsl:text> </xsl:text>
<xsl:value-of select="uc:user/uc:lastName" />:
</b>
<xsl:value-of select="uc:text" />
</li>
</xsl:template>
</xsl:stylesheet>
您可以将此xsl放在一个名为“userContent.xsl”的文件中,例如“userContent.xsl”,您可以将其放在根目录中。
现在我们需要整理您的代码:
<%
Option Explicit
Dim myRSSFile: myRSSfile = "http://abc.com"
Dim getPage: Set getPage = Server.CreateObject("MSXML2.XMLHTTPServer.3.0" )
getPage.Open "GET", myRSSfile, false
getPage.Send
Dim dom : dom = getPage.responseXml
Dim xsl : Set xsl = CreateObject("MSXML2.DOMDocument.3.0")
xsl.async = false
xsl.load Server.MapPath("/userContent.xsl")
%>
<html>
<head>
<title>Some Content</title>
</head>
<body>
<%
Response.Write dom.TransformNode(xsl)
%>
</body>
</html>
注意:
responseXml
属性,该属性为您提供要加载的XML文档而不是responseText
。 (当然不要使用RegEx来做)。Option Explicit
,这样可以节省您查找错误的时间。答案 1 :(得分:1)
我最终能够使用以下内容完成此操作,感谢各位帮助
<%
Dim objxml
Set objxml = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
objxml.async = False
objXML.setProperty "ServerHTTPRequest", True
objxml.load("http://abc.com")
set ElemUserContent=objxml.getElementsByTagName("userContent")
set ElemQuestion=objxml.getElementsByTagName("userContent/questions/question/text")
set ElemAnswer=objxml.getElementsByTagName("userContent/questions/question/answer")
set ElemFirstName=objxml.getElementsByTagName("userContent/questions/question/answer/user/firstName")
set ElemLastName=objxml.getElementsByTagName("userContent/questions/question/answer/user/lastName")
set ElemAnswerText=objxml.getElementsByTagName("userContent/questions/question/answer/text")
for x=0 to ElemUserContent.length-1
response.Write("<b>A Shopper Asked:</b> "&ElemQuestion.item(x).text)
response.Write("<ul>")
for i=0 to ElemAnswer.length-1
response.write("<li><b>"&ElemFirstName.item(i).text&" "&left(ElemLastName.item(i).text,1)&":</b> ")
response.Write(ElemAnswerText.item(i).text&"</li>")
next
response.Write("</ul>")
next
%>
答案 2 :(得分:0)
这当然是可能的。
我会尝试使用regular expressions。
要计算出表达式,有很多online tools。
编辑:经典的asp可以处理xml文档(我错误地认为它必须保留为文本,才能被解析) 这个例子:使用asp的Traverse XML文档可以帮助你,并且比我提供的reg ex选项更好的解决方案(没有完全考虑通过 - soz)