Read nested nodes in XML using Classic ASP

2019-07-13 11:14发布

My Classic ASP code connects to a URL and displays XML, my code looks like below

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 as follows

<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>

What i need to do is display
1) "questions/question/text" Tag
2) Display all the answers to that question that is this tag
"questions/question/answer/user/firstName" Tag
& 3) "questions/question/answer/text" Tag
Is it possible to do in Classic ASP?

3条回答
戒情不戒烟
2楼-- · 2019-07-13 11:26

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>

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.

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>

Notes:

  • 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 the responseText. (Certainly don't use RegEx to do it).
  • Always include Option Explicit in your scripts it will save you bags of time hunting bugs.
查看更多
forever°为你锁心
3楼-- · 2019-07-13 11:32

It's certainly possible.

I'd try to do it with regular expressions.

To work out the expression there are lots of online tools.

edit: classic asp can handle xml documents (I incorrectly thought it had to remain as text, to be parsed) This example: Traverse XML document using asp should help you and is a much better solution than the reg ex option I offered (without totally thinking it through - soz)

查看更多
家丑人穷心不美
4楼-- · 2019-07-13 11:40

I was able to do this using the following finally, thanks guys for all your help

<%
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
%>
查看更多
登录 后发表回答