简体   繁体   English

在经典ASP中读取XML

[英]Reading XML in classic ASP

I am trying to extract some XML into classic ASP for an old site. 我正在尝试将一些XML提取到旧站点的经典ASP中。

I can make it work for one example but not another. 我可以使它适用于一个示例,而不能适用于另一个示例。 I am wondering if anybody can let me know what I need to do to get them both running. 我想知道是否有人可以让我知道让他们两个都需要做的事情。 Thanks in advance. 提前致谢。

Working example 工作实例

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

Failing example 失败的例子

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

The failing example has an XML namespace set ( xmlns="http://api.freelancer.com/schemas/xml-0.1" ). 失败的示例具有XML名称空间集( xmlns="http://api.freelancer.com/schemas/xml-0.1" )。

All elements in this file are in that namespace. 该文件中的所有元素都在该命名空间中。 You must use it when you select nodes. 选择节点时必须使用它。

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

Notes 笔记

  • You can use the .load() method to load a file directly from an URL. 您可以使用.load()方法直接从URL加载文件。 There is no need for an extra ServerXMLHTTP object. 不需要额外的ServerXMLHTTP对象。
  • Always check the result of selectSingleNode() - it might be Nothing . 始终检查selectSingleNode()的结果-可能为Nothing
  • You should test for parse errors , too. 您也应该测试解析错误
  • You must use a namespace prefix, even if the document does not use one. 即使文档不使用名称空间前缀,也必须使用名称空间前缀。 You can choose whatever prefix you like as long as the namespace URIs match. 您可以选择所需的任何前缀,只要名称空间URI匹配即可。 For this example I chose fl . 在此示例中,我选择了fl
  • Use specific XPath expressions. 使用特定的XPath表达式。 /fl:profile/fl:url is better than //fl:url . /fl:profile/fl:url//fl:url更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM