解析在经典ASP中从wsdl WebService生成的XML字符串

时间:2014-02-04 16:42:42

标签: xml web-services parsing asp-classic wsdl

我想解析一个由webservice请求生成的XML字符串。 我的字符串是:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soapenv:Body>
    <getPersonBySgidResponse xmlns="http://all.service.xxxxx.com">
      <getPersonBySgidReturn>
        <alternateContactSgi xsi:nil="true"/>
        <birthDate>1986-11-14T22:00:00.327Z</birthDate>
        <birthDateOkay>true</birthDateOkay>
        <branchId>B78</branchId>
        <businessGroupId>R78E</businessGroupId>
        <contractEndDate xsi:nil="true"/>
        <contractStartDate>2013-09-16T22:00:00.327Z</contractStartDate>
        <contractorCompanyName xsi:nil="true"/>
        <countryId>FRA</countryId>
        <delegationId>DLFRA</delegationId>
        <departmentName>xxx</departmentName>
        <departmentNumber>XXXXX</departmentNumber>
        <detailed>true</detailed>
        <divisionName>XXXXO -  SIEGE SOCIAL</divisionName>
        <divisionNumber>31346</divisionNumber>
        <educationCompound xsi:nil="true"/>
        <employeePosition>N</employeePosition>
        <employeePositionLocal xsi:nil="true"/>
        <faxNumber xsi:nil="true"/>
        <filiereId>ADM</filiereId>
        <firstname>Lakhdar</firstname>
        <firstnameEncoded xsi:nil="true"/>
        <firstnamePreferred xsi:nil="true"/>
        <fullName>Lakhdar XXXX</fullName>
        <inChargeSgiJuridic xsi:nil="true"/>
        <inChargeSgiMission xsi:nil="true"/>
      </getPersonBySgidReturn>
    </getPersonBySgidResponse>
  </soapenv:Body>
</soapenv:Envelope>

我必须在“Response.Write”之前添加一个标签,以便能够像这样看到XML(否则字符串只是所有属性的串联)。

我把这个字符串放在“MyXml”变量中,我试着像这样解析它:

Dim objXML,objRoot ,I, thisNode,strID, strNarrative, thisChild, selectedNode,testStr

    Set objXML= Server.CreateObject("MSXML2.DOMDocument") 
    objXML.async = False    

    objXML.setProperty "SelectionLanguage", "XPath" 
    objXML.setProperty "SelectionNamespaces", "xmlns='http://all.service.xxxxx.com' " & _
        "xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' " & _
        "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " & _
        "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "

    objXML.loadXML(myXML) 
    Set selectedNode= objXML.selectSingleNode("//soapenv:Envelope/soapenv:Body/getPersonBySgidResponse/getPersonBySgidReturn/getPersonBySgidReturn/divisionName")

    Set objRoot = objXML.documentElement 

    Response.Write "<br/> myXml == "  &  myXML
    Response.Write "<br/> objXML == "  &  objXML.Text   
    Response.Write "<br/> objRoot == "  &  objRoot.Text
    Response.Write "<br/> selectedNode == "  &  selectedNode.Text

但是在最后一行我得到了错误:需要对象。

希望我写的内容有道理:) 感谢

1 个答案:

答案 0 :(得分:0)

错误的XPath "//soapenv:Envelope/soapenv:Body/getPersonBySgidResponse/getPersonBySgidReturn/getPersonBySgidReturn/divisionName"

从XPath中删除一个getPersonBySgidReturn

您的代码行应

Set selectedNode= objXML.selectSingleNode("//soapenv:Envelope/soapenv:Body/getPersonBySgidResponse/getPersonBySgidReturn/divisionName")