VBScript Can not Select XML nodes

前端 未结 3 1148
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 18:00

I am trying to Select nodes from some webservice response XML to no avail. For some reason I am able to select the root node (\"xmldata\") however, when I try to drill deeper(\"

3条回答
  •  醉梦人生
    2021-01-28 18:22

    Short answer

    oXMLHttp.ResponseXML.text may return some text, but not 'A string containing a URL that specifies the location of the XML file' as required for the parameter of .load. So replace

    xmlDoc.load (oXMLHttp.ResponseXML.text)
    

    with

    xmlDoc.loadXml oXMLHttp.ResponseXML.xml
    

    If that 'does not work', say so; I then will try to provide a longer answer.

    (P.S. to the short answer: AnthonyWJones' advice not to transform the XML twice is sound; I offered this 'minimal impact on existing code' approach in the hope to get the OT over the first hurdle, not as a generally applicable strategy.)

    Longer answer

    If you have XML problems on an ASP page, you should try to isolate and test the XML specific problems in a console script. For your problem I filled a skeleton (load .xml file, check for errors) with code to access nodes via XPath and DOM tree:

      Dim oFS    : Set oFS  = CreateObject( "Scripting.FileSystemObject" )
      Dim sFSpec : sFSpec   = oFS.GetAbsolutePathName("..\data\00.xml")
      Dim oXml   : Set oXml = CreateObject("Msxml2.DOMDocument")
    
      oXml.setProperty "SelectionLanguage", "XPath"
      oXml.async = False
      oXml.load sFSpec
    
      If 0 = oXml.parseError.errorCode Then
         WScript.Echo "loaded:", sFSpec
         WScript.Echo "root:", oXml.documentElement.tagName
    
         Dim sXPath, ndlFnd, ndChild, ndFnd
    
         sXPath = "/xmldata/customers"
         Set ndlFnd = oXml.selectNodes(sXPath)
         If 0 = ndlFnd.length Then
            WScript.Echo "no '" & sXPath & "' found"
         Else
            WScript.Echo "found", ndlFnd.length, "node(s) for '" & sXPath & "'"
            sXPath = "firstname"
            For Each ndChild In ndlFnd
                WScript.Echo "child:", ndChild.tagName
                Set ndFnd = ndChild.selectSingleNode(sXPath)
                If ndFnd Is Nothing Then
                   WScript.Echo "no '" & sXPath & "' found"
                Else
                   WScript.Echo ndFnd.text, "==", ndChild.childNodes(1).text
                End If
            Next
         End If
      Else
         WScript.Echo "errorCode:", oXml.parseError.errorCode
         WScript.Echo oXml.parseError.reason
      End If
    

    output:

    loaded: E:\trials\SoTrials\answers\11166940\data\00.xml
    root: xmldata
    found 1 node(s) for '/xmldata/customers'
    child: customers
    Jim == Jim
    

    As you can see

    1. I use standard/approved methods to check the result of the single steps (e.g. parseError (instead of voodoo length test) to see if I got a well-formed/valid/usable document)
    2. When in doubt, I put in a WScript.Echo to make sure my assumptions about what VBScript should deliver hold water.

提交回复
热议问题