Asp XML Parsing

不打扰是莪最后的温柔 提交于 2020-01-09 10:54:10

问题


I am new to asp and have a deadline in the next few days. i receive the following xml from within a webservice response.

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>");

How can i parse this xml into asp attributes?

Any help is greatly appreciated

Thanks Damien

On more analysis, some soap stuff is also returned as the aboce response is from a web service call. can i still use lukes code below?


回答1:


You need to read about MSXML parser. Here is a link to a good all-in-one example http://oreilly.com/pub/h/466

Some reading on XPath will help as well. You could get all the information you need in MSDN.

Stealing the code from Luke excellent reply for aggregation purposes:

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



回答2:


By ASP I assume you mean Classic ASP? Try:

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

The above code assumes you have your XML in a variable called sXML. If you are consuming this via an ServerXMLHttp request, you should be able to use the ResponseXML property of your object in place of oXML above and skip the LoadXML step altogether.




回答3:


You could try loading the xml into the xmldocument object and then parse it using it's methods.



来源:https://stackoverflow.com/questions/94689/asp-xml-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!