XML 和 ASP:检索和解析远程文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/902554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML and ASP: Retrieve and parse a remote file
提问by NatalieMac
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:
我正在启用 ASP 的 Windows Server 上构建站点。我需要从另一台服务器检索 XML 文档并返回该文档中的值。xml 文件很小 - 只有一个带有文本值的节点。我只需要返回那个文本值。我以前从未使用过 ASP,并且通过谷歌搜索使我找到了一些代码示例,但到目前为止没有任何效果。这是我得到的,它给了我 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:
是的,您对我的 XML 完全正确。只有一个具有值的节点。根据您的评论,我将我的 asp 代码编辑为:
<%
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?
哪个仍然返回 500。我该如何调试 ASP?有没有办法打开详细的错误报告?
回答by juan
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>
回答by AnthonyWJones
Assuming your Xml is in fact:-
假设您的 Xml 实际上是:-
<?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
此外,如果这是您的服务器,请安装 MSXML6 并使用 MSXML2.DOMDocument.6.0。如果这不是您的服务器,请使用 MSXML3.DOMDocument.3.0
回答by mike nelson
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 hereif you need that.
如果您仍然收到 500 个错误,它们可能来自 IIS(您可以通过查看它们来判断)。如果您需要,我已经在此处提供了在 IIS7 上启用错误消息的指南。
回答by jammus
Change line 4 of your original snippet to
将原始代码段的第 4 行更改为
Set objXML = Server.CreateObject("MSXML2.DOMDocument.6.0")
and line 14 to
和第 14 行到
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 是 AnthonyWJones 描述的)。
您原来的 //xml/response 将从看起来像这样的文档中获取文本
<?xml version="1.0" ?>
<xml>
<response>hello</response>
</xml>
回答by ewbi
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 developingand/or supportingclassic ASP, enabling debugging can be tricky.
经典的 ASP 调试是一个令人讨厌的话题,多年来,数以百万计的优秀脑细胞已经被牺牲了。即使使用旨在开发和/或支持经典 ASP 的工具,启用调试也可能很棘手。
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'sanswer, 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.
如果您的努力是相对较小的一次性事情,正如您的问题所暗示的那样,那么花费大量时间设置和配置高级 ASP/脚本调试环境可能没有意义。相反,根据Pete Duncanson 的回答,只需将一些 Response.Write 语句注入您的脚本并找出它在哪里以及为什么以老式方式失败。但是,Pete 没有指出的一件事是,您需要打开 VBScript 错误处理程序(实际上是错误吞咽器)以避免抛出未处理的异常,从而导致 IIS 为您提供 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'sanswer. (Btw, I have no idea how you got your VBScript so well formatted in the original question, so my copy looks pretty bad.)
我设置并运行了以下代码,它运行良好(即没有错误)。XML URL 指向本地机器上与 ASP 页面相同的虚拟目录中的一个简单文件,它包含在AnthonyWJones 的回答中找到的 XML 。(顺便说一句,我不知道您是如何在原始问题中将 VBScript 格式化得如此好,所以我的副本看起来很糟糕。)
<%
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:
在 IE 或 Firefox 中打开它,如果一切顺利,你应该看到:
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 informationabout the VBScript Err object.
当然,一切都不会顺利,否则您就不会在这里,在这种情况下,您应该看到错误详细信息出现在 Response.Write 值之一之后的某个点。下面是有关 VBScript Err 对象的一些附加信息。
Good luck!
祝你好运!
回答by Pete Duncanson
Debugging ASP is not as pleasant as you might be used to. This should help though:
调试 ASP 并不像您习惯的那样愉快。不过,这应该会有所帮助:
- 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.
- 如果使用 IE,请确保在选项中取消勾选“显示友好错误消息”
- 使用 Response.Write's 来跟踪您完成代码的程度。
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.
可能是您正在使用的服务器上有一个 500 错误处理程序页面(假设您没有在本地运行)。在这种情况下,如果可以,您将不得不修改 500 页,以便它为您提供有关实际错误的更多详细信息(请参阅http://www.w3schools.com/ASP/asp_ref_error.asp)。如果您在本地开发,尽管您往往会获得所有有趣的细节。

