XML and ASP: Retrieve and parse a remote file

2019-02-15 04:53发布

I'm building a site on a Windows Server with ASP enabled. I need to retrieve an XML document from another server and return a value in that document. The xml file is small - only one node with a text value. I just need to return that text value. I've never worked with ASP before, and Googling around has led me to some code examples, but nothing that works so far. Here's what I've got, which gives me a 500:

<%
Dim URL, objXML
URL = "http://someserver.com/xml"
Set objXML = Server.CreateObject("MSXML2.DOMDocument.4.0")
objXML.setProperty "ServerHTTPRequest", True
objXML.async =  False
objXML.Load(URL)

If objXML.parseError.errorCode <> 0  Then
    Response.Write(objXML.parseError.reason)
    Response.Write(objXML.parseError.errorCode)
End If

Set oRoot = objXML.selectSingleNode("//xml/response")
var = oRoot.text

set objXML = nothing
%>

<%= var %>

===========

Update:

Yes, you're exactly correct about my XML. Just one node with a value. Based on your comments, I edited my asp code to:

<%

Dim URL, objXML, value
URL = "http://someserver.com/xml"
Set objXML = Server.CreateObject("MSXML2.DOMDocument.6.0")
objXML.setProperty "ServerHTTPRequest", True
objXML.async =  False
objXML.Load URL

Response.Write objXML.parseError.reason

value = objXML.documentElement.Text

set objXML = nothing

%>

<%= value %>

Which is still returning a 500. How do I go about debugging ASP? Is there some way to turn on detailed error reporting?

6条回答
来,给爷笑一个
2楼-- · 2019-02-15 05:26

Debugging ASP is not as pleasant as you might be used to. This should help though:

  • If using IE ensure you have unticked "Show friendly error messages" in the options
  • Use Response.Write's to track just how far you are getting through your code.

It could be that you have a 500 error handler page on the server you are using (assuming you are not running local). In which case you will have to modify the 500 page if you can so it gives you more details of the real error (see http://www.w3schools.com/ASP/asp_ref_error.asp). If you develop locally though you tend to get all the juicy details.

查看更多
何必那么认真
3楼-- · 2019-02-15 05:30

I wrote this function:

<%
   Option Explicit
   Response.Buffer = True
   Dim xml
   Set xml = Server.CreateObject("Microsoft.XMLDOM")
   xml.async = False
   xml.setProperty "ServerHTTPRequest", True
   xml.Load ("http://www.midominio.com/nombre.xml")
   Dim title, heading, paragraph, testHTML
   title = xml.documentElement.childNodes(0).text
   heading = xml.documentElement.childNodes(1).text
   paragraph = xml.documentElement.childNodes(2).text
   testHTML = xml.documentElement.childNodes(3).text
   Set xml = Nothing
%>
<html>
   <head>
   <title><%= title %></title>
   </head>
   <body>
   <h3 align="center"><%= heading %></h3>
   <p align="center"><% = paragraph %></p>
   <div align="center"><%= testHTML %></div>
   </body>
</html>
查看更多
萌系小妹纸
4楼-- · 2019-02-15 05:38

As Pete Duncanson said, the first thing to try is to untick "Show friendly error messages".

If you are still getting 500 errors they are probably coming from IIS (you can probably tell by looking at them). I have put up a guide for enabling error messages on IIS7 here if you need that.

查看更多
相关推荐>>
5楼-- · 2019-02-15 05:44

Change line 4 of your original snippet to

Set objXML = Server.CreateObject("MSXML2.DOMDocument.6.0")

and line 14 to

Set oRoot = objXML.selectSingleNode("//response")

and you should be fine (assuming your xml is as AnthonyWJones describes).

Your original //xml/response would get the text from a document that looked like this

<?xml version="1.0" ?>
<xml>
    <response>hello</response>
</xml>
查看更多
太酷不给撩
6楼-- · 2019-02-15 05:50

Classic ASP debugging is a nasty topic to which millions of otherwise fine brain cells have been sacrificed over the years. Even with tools intended for developing and/or supporting classic ASP, enabling debugging can be tricky.

If your effort is a relatively small one-time thing, as your question sort of suggests, then it probably doesn't make sense to spend a lot of time setting up and configuring an advanced ASP/script debugging environment. Instead, as per Pete Duncanson's answer, simply inject some Response.Write statements into your script and figure out where and why it is failing the old fashioned way. However, the one thing Pete didn't point out is that you'll need to turn on a VBScript error handler (error swallower, actually) in order to avoid tossing an unhandled exception, resulting in IIS serving you a 500.

I setup and ran the following code and it worked fine (i.e., no errors). The XML URL pointed to a simple file in the same virtual directory on the local machine as the ASP page and it contained the XML found in AnthonyWJones's answer. (Btw, I have no idea how you got your VBScript so well formatted in the original question, so my copy looks pretty bad.)

<%
On Error Resume Next  ' prevent tossing unhandled exception
Dim URL, objXML, value
URL = "http://someserver.com/xml"
Set objXML = Server.CreateObject("MSXML2.DOMDocument.6.0")
Response.Write "after CreateObject: " & Err.Description & "<br>"
objXML.setProperty "ServerHTTPRequest", True
Response.Write "after setProperty: " & Err.Description & "<br>"
objXML.async =  False
Response.Write "after async: " & Err.Description & "<br>"
objXML.Load URL
Response.Write "after Load: " & Err.Description & "<br>"

Response.Write objXML.parseError.reason
Response.Write "after write of parseError.reason: " & Err.Description & "<br>"

value = objXML.documentElement.Text
Response.Write "after setting value: " & Err.Description & "<br>"

set objXML = nothing

%>

<%= value %>

Open this in IE or Firefox and if everything goes well you should see this:

after CreateObject: 
after setProperty: 
after async: 
after Load: 
after write of parseError.reason: 
after setting value: 
The value

Of course, everything isn't going to go well, otherwise you wouldn't be here, in which case you should see the error details appear at some point following one of the Response.Write values. Here's some additional information about the VBScript Err object.

Good luck!

查看更多
\"骚年 ilove
7楼-- · 2019-02-15 05:51

Assuming your Xml is in fact:-

<?xml version="1.0" ?>
<response>The value</response>

Try using:-

Dim value
value = objXML.documentElement.Text

BTW,

When you call methods from which you are not returning a value you do not need the brackets:-

objXML.Load Url

Response.Write objXML.parseError.reason

Also if this is your server, install MSXML6 and use MSXML2.DOMDocument.6.0. IF this is not your server use MSXML3.DOMDocument.3.0

查看更多
登录 后发表回答