【问题标题】:Reading XML in classic ASP在经典 ASP 中读取 XML
【发布时间】:2013-11-14 15:22:55
【问题描述】:

我正在尝试将一些 XML 提取到旧网站的经典 ASP 中。

我可以使它适用于一个示例,但不能适用于另一个示例。我想知道是否有人可以让我知道我需要做什么才能让它们都运行。提前致谢。

工作示例

Dim o2, oXML2
Set oXML2 = Server.CreateObject("Msxml2.DOMDocument.6.0")
Set o2 = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
o2.open "GET", "https://api.eveonline.com/eve/CharacterID.xml.aspx?names=BorisKarlov", False
o2.send


xml2 = o2.responseText
oXML2.LoadXML xml2

response.Write oXML2.selectSingleNode("//currentTime").Text

失败的例子

Dim o, oXML
Set oXML = Server.CreateObject("Msxml2.DOMDocument.6.0")
Set o = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
o.open "GET", "http://api.freelancer.com/User/Properties.xml?id=sulung81", False
o.send

xml = o.responseText
oXML.LoadXML xml

response.Write oXML.selectSingleNode("//url").Text

【问题讨论】:

  • 而且,它怎么不起作用?你得到什么?你认为你应该得到什么?有什么错误吗?
  • 非常感谢您在这方面的帮助。我收到错误消息:msxml6.dll 错误“80004005”尝试修改只读节点。与行有关:oXML.setProperty "SelectionNamespaces", "xmlns:fl='api.freelancer.com/schemas/xml-0.1'"

标签: xml asp-classic


【解决方案1】:

失败的示例有一个 XML 命名空间集 (xmlns="http://api.freelancer.com/schemas/xml-0.1")。

此文件中的所有元素都在该命名空间中。选择节点时必须使用它。

Dim oXML, node
Set oXML = Server.CreateObject("Msxml2.DOMDocument.6.0")

oXML.load "http://api.freelancer.com/User/Properties.xml?id=sulung81"
oXML.setProperty "SelectionNamespaces", "xmlns:fl='http://api.freelancer.com/schemas/xml-0.1'"

Set node = oXML.selectSingleNode("/fl:profile/fl:url")

If Not node Is Nothing
    Response.Write node.Text
End If

注意事项

  • 您可以使用the .load() method 直接从 URL 加载文件。不需要额外的 ServerXMLHTTP 对象。
  • 始终检查 selectSingleNode() 的结果 - 可能是 Nothing
  • 您也应该测试parse errors
  • 您必须使用命名空间前缀,即使文档没有使用。只要命名空间 URI 匹配,您就可以选择任何您喜欢的前缀。对于这个例子,我选择了fl
  • 使用特定的 XPath 表达式。 /fl:profile/fl:url 优于 //fl:url

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多