5

我是asp的新手,在接下来的几天里有一个截止日期。我从 web 服务响应中收到以下 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">john.doe@johnmail.com</attribute>
</person_info>
</user_data>");

如何将此 xml 解析为 asp 属性?

任何帮助是极大的赞赏

谢谢达米安

进一步分析,由于 aboce 响应来自 Web 服务调用,因此还会返回一些肥皂内容。我还可以使用下面的 lukes 代码吗?

4

3 回答 3

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
于 2008-09-18T17:32:21.520 回答
6

通过 ASP,我假设您的意思是 Classic 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 步骤。

于 2008-09-18T17:36:33.070 回答
0

您可以尝试将 xml 加载到 xmldocument 对象中,然后使用它的方法对其进行解析。

于 2008-09-18T17:33:56.897 回答