使用传统的ASP在XML阅读嵌套节点 [英] Read nested nodes in XML using Classic ASP
本文介绍了使用传统的ASP在XML阅读嵌套节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的经典ASP code连接到一个URL,并显示XML,我的code看起来像下面
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)显示所有的答案,这个问题就是这个标签结果
问题/问题/答案/用户/名字标签结果
&安培;
3)的问题/问题/答案/文本标签结果
是否有可能在经典ASP办?
推荐答案
该方法采取与本就是使用XSL来执行对接收的XML转换来生成要显示HTML。这里是一个样本,让你开始:
The approach to take with this is to use XSL to perform a transform on the received XML to generate the HTML you would like to display. Here is a sample to get you started:
<?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的地方文件,为便于例如我们把它放在根目录下。
You would place this xsl in a file called for example "userContent.xsl" somewhere in you web site, for ease of example we'll put it in the root.
现在,我们需要整理你的code:
Now we need to tidy your code:
<%
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>
注:
- 请不要使用XMLHTTP在ASP它不是设计在服务器中使用。
- 有没有需要发送内容类型头在XML HTTP请求时,你是不是的发送的任何内容。
- 当你想操作XML收到的使用,让你加载XML文档,而不是
的responseText $玩
responseXML的
属性C $ C>。 (当然,不使用正则表达式来做到这一点)。 - 始终包括
选项显式
在脚本中,将节省您的时间狩猎错误的包装袋。
- Don't use XMLHTTP in ASP it isn't designed to be used in a Server.
- There is no need to send a content type header in xml http request when you aren't sending any content.
- When you want to manipulate XML received use the
responseXml
property which gives you a loaded XML Document to play with rather than theresponseText
. (Certainly don't use RegEx to do it). - Always include
Option Explicit
in your scripts it will save you bags of time hunting bugs.
这篇关于使用传统的ASP在XML阅读嵌套节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文