ASP XML解析
时间:2020-03-06 14:22:29 来源:igfitidea点击:
我是新手,并且在接下来的几天有最后期限。
我从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">[email protected]</attribute> </person_info> </user_data>");
我如何将这个xml解析为asp属性?
任何帮助是极大的赞赏
谢谢
达米安
在更多分析中,由于重新响应来自Web服务调用,因此还会返回一些消息。我仍然可以使用下面的lukes代码吗?
解决方案
我们需要阅读有关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
我们可以尝试将xml加载到xmldocument对象中,然后使用其方法进行解析。
通过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步骤。