【问题标题】:Parse XML using classic asp and xpath使用经典的 asp 和 xpath 解析 XML
【发布时间】:2014-07-02 07:14:28
【问题描述】:

我正在尝试使用经典 asp 在我的 XML 中读取我的“保存”节点,它给了我一个错误,我知道某处有一个小问题,我似乎无法弄清楚....有什么想法吗? 这是所需的错误对象:'order.item(...)'

<%
    Dim email,template,featured, monthx 

    email = "email@example.com"
    monthx = "week1"
    featured = "featured"

    set objXML = Server.CreateObject("MSXML2.DOMDocument")
    objXML.async = false    
    objXML.load(Server.MapPath("records.xml"))

    'Find if order exists
    xPath = "//rep[@email='"&email&"']/month[@name='" & monthx & "']/product[@type='" & featured & "']/saving"
    set order = objXML.selectNodes(xPath)   


        retStr = retStr & order.item(i).selectSingleNode("saving").text & ","

        Response.Write(retStr)


    set objXML = nothing    
%>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<replist>
  <rep email="email@example.com">
    <month name="Week1">
      <product type="featured">
        <model>Honda</model>
        <i>G</i>
        <p>7</p>
        <e>AA</e>
        <sn>123432</sn>
        <saving>save 30</saving>
      </product>
    </month>
  </rep>
</replist>

【问题讨论】:

    标签: xml xpath vbscript asp-classic


    【解决方案1】:

    阅读本文并修复您的错误(下次发布时附上编号、行、描述):

    email = "email@example.com" ' not example@example.com
    monthX = "Week1"  ' can't use name of Month function as variable name
    featured = "featured"
    
    set objXML = CreateObject("MSXML2.DOMDocument")
    objXML.async = false
    objXML.load "24517985.xml"
    
    If objXML.parseError Then ' never without my check
       WScript.Echo objXML.parseError.reason
    Else
       'Find list of all ...saving nodes
       xPath = "//rep[@email='"&email&"']/month[@name='" & monthX & "']/product[@type='" & featured & "']/saving"
       set order = objXML.selectNodes(xPath)
       If 0 = order.length Then ' never without my check
          WScript.Echo "fail:", xPath
       Else
    '     WScript.Echo order.item(i).selectSingleNode("saving").text & ","
    '     we got a ...saving, no need for this rigmarole
          WScript.Echo order(0).tagName, order(0).text
       End If
    End If
    

    输出:

    cscript 24517985.vbs
    saving save 30
    

    更新问题的编辑:

    导致错误:“需要对象:'order.item(...)'”

    • 通过 selectSingleNode() 返回一个空/0 长度节点集合,因为
    • XPath 查询失败,因为我的 cmets 中提到了各种错误
    • 不检查故障

    【讨论】:

    • 我收到此错误......需要对象:'WScript',我认为它应该是 response.write 而不是“Echo”,你不觉得吗?
    • 我认为您还需要在其中添加此设置 objXML = nothing
    • @mmz - 我说的是“阅读”,而不是“复制”。不,我不需要将对象设置为 Nothing。
    • 即使在解决了您突出显示的问题之后,我仍然遇到错误,并且您的代码语法有点错误。即 WScript.Echo.
    猜你喜欢
    • 2012-07-02
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多