2008-09-18 60 views
5

我是asp的新手,在接下來的幾天內會有一個截止日期。 我從Web服務響應中收到以下xml。Asp XML解析

print("<?xml version="1.0" encoding="UTF-8"?> 
<user_data> 
<execution_status>0</execution_status> 
<row_count>1</row_count> 
<txn_id>stuetd678</txn_id> 
<person_info> 
    <attribute name="firstname">john</attribute> 
    <attribute name="lastname">doe</attribute> 
    <attribute name="emailaddress">[email protected]</attribute> 
</person_info> 
</user_data>"); 

如何解析這個XML到ASP屬性?

任何幫助是極大的讚賞

感謝 達明

在更多的分析,也返回了一些肥皂的東西作爲aboce響應來自Web服務調用。我仍然可以使用下面的lukes代碼嗎?

回答

9

您需要閱讀有關MSXML分析器的內容。這裏是一個很好的一體化例子的鏈接http://oreilly.com/pub/h/466

一些在XPath上的閱讀也會有幫助。你可以在MSDN中獲得你需要的所有信息。

盜竊Luke優秀答覆代碼聚集的目的:

Dim oXML, oNode, sKey, sValue 

Set oXML = Server.CreateObject("MSXML2.DomDocument.6.0") 'creating the parser object 
oXML.LoadXML(sXML) 'loading the XML from the string 

For Each oNode In oXML.SelectNodes("/user_data/person_info/attribute") 
    sKey = oNode.GetAttribute("name") 
    sValue = oNode.Text 
    Select Case sKey 
    Case "execution_status" 
    ... 'do something with the tag value 
    Case else 
    ... 'unknown tag 
    End Select 
Next 

Set oXML = Nothing 
+0

我不明白..你從來不用sValue做什麼? – JoJo 2012-09-05 15:28:41

0

你可以嘗試加載XML到XmlDocument的對象,然後使用它的方法解析它。

6

通過ASP我假設你的意思是經典的ASP?試試:

Dim oXML, oNode, sKey, sValue 

Set oXML = Server.CreateObject("MSXML2.DomDocument.4.0") 
oXML.LoadXML(sXML) 

For Each oNode In oXML.SelectNodes("/user_data/person_info/attribute") 
    sKey = oNode.GetAttribute("name") 
    sValue = oNode.Text 
    ' Do something with these values here 
Next 

Set oXML = Nothing 

上面的代碼假設你有你的XML在一個名爲sXML的變量。如果您通過ServerXMLHttp請求來使用它,則應該能夠使用對象的ResponseXML屬性代替上述的oXML,並完全跳過LoadXML步驟。