2013-11-14 63 views
0

我正在嘗試將一些XML抽取到舊網站的經典ASP中。在經典ASP中讀取XML

我可以使它適用於一個例子,但不是另一個例子。我想知道是否有人能讓我知道我需要做些什麼才能讓它們都運行。提前致謝。

工作實施例

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 
+0

而且,它不起作用?你得到了什麼?你認爲你應該得到什麼?有沒有錯誤? –

+0

非常感謝您對此的幫助。我收到錯誤:msxml6.dll錯誤'80004005' 嘗試修改只讀節點。 相關的行: oXML.setProperty「SelectionNamespaces」,「xmlns:fl ='http://api.freelancer.com/schemas/xml-0.1'」 – tommarshallandrews

回答

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
+0

非常感謝您對此的幫助。我收到錯誤 – tommarshallandrews