Asp XML 解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/94689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:02:56  来源:igfitidea点击:

Asp XML Parsing

xmlparsingasp-classic

提问by Damien

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

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

How can i parse this xml into asp attributes?

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

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?

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

回答by Ilya Kochetov

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

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

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

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

Stealing the code from Lukeexcellent reply for aggregation purposes:

出于聚合目的,从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

回答by Luke Bennett

By ASP I assume you mean Classic ASP? Try:

通过 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

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.

上面的代码假设您的 XML 位于名为 sXML 的变量中。如果您通过 ServerXMLHttp 请求使用它,您应该能够使用对象的 ResponseXML 属性代替上面的 oXML 并完全跳过 LoadXML 步骤。

回答by Paulj

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

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