简体   繁体   中英

Reading XML in classic ASP

I am trying to extract some XML into classic ASP for an old site.

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" ).

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. There is no need for an extra ServerXMLHTTP object.
  • Always check the result of selectSingleNode() - it might be 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. For this example I chose fl .
  • Use specific XPath expressions. /fl:profile/fl:url is better than //fl:url .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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