【问题标题】:Asp XML ParsingAsp XML 解析
【发布时间】:2010-09-10 19:35:22
【问题描述】:

我是 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 代码吗?

【问题讨论】:

    标签: xml parsing asp-classic


    【解决方案1】:

    您需要阅读有关 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
    

    【讨论】:

    • 我不明白.. 你从来没有对 sValue 做过任何事??
    【解决方案2】:

    我假设你的意思是经典 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 步骤。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2012-04-27
        • 1970-01-01
        • 2018-12-09
        • 2012-07-02
        • 1970-01-01
        • 1970-01-01
        • 2012-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多