user_data
execution_status0/execution_status
row_count1/row_count"/>

Asp XML 解析

Asp XML 解析

Asp XML Parsing

我是 asp 新手,在接下来的几天里有一个截止日期。
我从 Web 服务响应中收到以下 xml。

1
2
3
4
5
6
7
8
9
10
11
print("?xml version="1.0" encoding="UTF-8"?
user_data
execution_status0/execution_status
row_count1/row_count
txn_idstuetd678/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 代码吗?


您需要阅读有关 MSXML 解析器的信息。这是一个好的多合一示例的链接 http://oreilly.com/pub/h/466

阅读 XPath 也会有所帮助。您可以在 MSDN 中获得所需的所有信息。

从 Luke 的优秀回复中窃取代码用于聚合目的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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


我假设你的意思是经典的 ASP?试试:

1
2
3
4
5
6
7
8
9
10
11
12
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 步骤。


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


推荐阅读