0

我有一个带有函数的 VBScript,它将返回 XML 作为输出。如何读取输出文件并从 xml 中获取标签 (emp_id) 中的值?

strResult = myfunction (Str1,Str2)

Set objXML = Server.CreateObject("Msxml2.DOMDocument")
objXML.LoadXml(strResult)

当我尝试加载 Xml 时,Server 和 objXml 为空。

strResult 包含一个带有许多标签的 xml。其中我只对一个标签(emp_id)感兴趣

在'strResult'中输出xml

<?xml version="1.0" encoding="UTF-8"?>
<NewDataSet>
 <xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns="">
 <xs:element name="NewDataSet" msdata:IsDataSet="true"><xs:complexType><xs:choice maxOccurs="unbounded">
 <xs:element name="ReturningDataSet"><xs:complexType><xs:sequence><xs:element name="sno" type="xs:int" minOccurs="0"/>
<xs:element name="alternate_id" type="xs:string" minOccurs="0"/>
<xs:element name="cusip" type="xs:string" minOccurs="0"/>
<xs:element name="alternate_id_type_cd" type="xs:string" minOccurs="0"/>
<xs:element name="isin" type="xs:string" minOccurs="0"/>
<xs:element name="emp_id" type="xs:string" minOccurs="0"/>
<xs:element name="sedol" type="xs:string" minOccurs="0"/>
<xs:element name="issuer_id" type="xs:string" minOccurs="0"/>
<xs:element name="iv_type" type="xs:string" minOccurs="0"/>
<xs:element name="idType" type="xs:string" minOccurs="0"/>
<xs:element name="couponRate" type="xs:double" minOccurs="0"/>
<xs:element name="maturityDt" type="xs:dateTime" minOccurs="0"/>
 <xs:element name="datedDt" type="xs:dateTime" minOccurs="0"/>
 <xs:element name="branch" type="xs:string" minOccurs="0"/>
<xs:element name="securityName" type="xs:string" minOccurs="0"/>
<xs:element name="iso_currency" type="xs:string" minOccurs="0"/>
<xs:element name="dayCountPer" type="xs:string" minOccurs="0"/>
<xs:element name="dayCountYr" type="xs:string" minOccurs="0"/>
<xs:element name="marketExch" type="xs:string" minOccurs="0"/>
<xs:element name="inputPrcForm" type="xs:string" minOccurs="0"/>
<xs:element name="iso_country" type="xs:string" minOccurs="0"/>
<xs:element name="settlementConv" type="xs:string" minOccurs="0"/>
<xs:element name="settlementMeth" type="xs:string" minOccurs="0"/>
<xs:element name="firstCpnDt" type="xs:dateTime" minOccurs="0"/>
<xs:element name="securityMinDen" type="xs:double" minOccurs="0"/>
<xs:element name="securityMinInc" type="xs:double" minOccurs="0"/>
<xs:element name="country" type="xs:string" minOccurs="0"/>
<xs:element name="currency" type="xs:string" minOccurs="0"/>
<xs:element name="programId" type="xs:string" minOccurs="0"/>
<xs:element name="programName" type="xs:string" minOccurs="0"/>
<xs:element name="warningMessages" type="xs:string" minOccurs="0"/>
                                </xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType>
                                </xs:element></xs:schema><ReturningDataSet id="ReturningDataSet1" rowOrder="0">
                                <sno>1</sno>
                                <alternate_id>02R99BBM5                     </alternate_id>
                                <cusip>            </cusip>
                                <alternate_id_type_cd>FMRID </alternate_id_type_cd>
                                <isin>            </isin>
                                ****<emp_id>02R349BXC5</emp_id>****
                                <sedol>       </sedol>
                                <issuer_id>02R99B</issuer_id>
                                <iv_type>YCD </iv_type>
                                <idType>FMRID </idType>
                                <couponRate>0.7</couponRate>
                                <maturityDt>2012-12-26T00:00:00.0000000</maturityDt>
                                <datedDt>2012-12-11T00:00:00.0000000</datedDt>
                                <branch>NYC </branch>
                                <securityName>ASB Y$CD .7% 12/26/12         </securityName>
                                <iso_currency>USD </iso_currency>
                                <dayCountPer>ACT</dayCountPer>
                                <dayCountYr>360</dayCountYr>
                                <marketExch>OT</marketExch>
                                <inputPrcForm>C                             </inputPrcForm>
                                <iso_country>NZ  </iso_country>
                                <settlementConv>DT</settlementConv>
                                <settlementMeth>DTCF           </settlementMeth>
                                <country>NEWZ</country>
                                <currency>USA</currency>
                                <programId>            </programId>
                                <programName>        </programName>
<warningMessages>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;NewDataSet&gt;&lt;xs:schema id="NewDataSet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns=""&gt;&lt;xs:element name="NewDataSet" msdata:IsDataSet="true"&gt;&lt;xs:complexType&gt;&lt;xs:choice maxOccurs="unbounded"&gt;&lt;xs:element name="ReturningDataSet"&gt;&lt;xs:complexType&gt;&lt;xs:sequence&gt;&lt;xs:element name="errorSev" type="xs:string" minOccurs="0"/&gt;&lt;xs:element name="errorId" type="xs:string" minOccurs="0"/&gt;&lt;xs:element name="errorText" type="xs:string" minOccurs="0"/&gt;&lt;/xs:sequence&gt;&lt;/xs:complexType&gt;&lt;/xs:element&gt;&lt;/xs:choice&gt;&lt;/xs:complexType&gt;&lt;/xs:element&gt;&lt;/xs:schema&gt;&lt;ReturningDataSet id="ReturningDataSet1" rowOrder="0"&gt;&lt;errorSev&gt;20&lt;/errorSev&gt;&lt;errorId&gt;CDA1356&lt;/errorId&gt;&lt;errorText&gt;FDWarning1: Freq of interest Payment not available from prefered data source FREQ_RATE_CHANGE_CD. Data system defaulted.            &lt;/errorText&gt;&lt;/ReturningDataSet&gt;&lt;/NewDataSet&gt;</warningMessages>
</ReturningDataSet>
</NewDataSet>
4

1 回答 1

1

创建一个Msxml2.DOMDocument,配置它(例如使用 XPath),使用它的 .loadXml 方法来加载您的函数返回/XML 代码(片段),并通过 XPATH(可能是 .selectSingleNode)访问您感兴趣的项目。

(我相信不需要文件。)

添加:

“有效”的东西:

  Dim sXML   : sXML      = "<emp><empid>123</empid></emp>"
  Dim oXDoc  : Set oXDoc = CreateObject( "Msxml2.DOMDocument.6.0" )
  Dim sXPath : sXPath    = "/emp/empid"
  oXDoc.setProperty "SelectionLanguage", "XPath"
  oXDoc.async = False
  oXDoc.loadXml sXML

  If 0 = oXDoc.ParseError Then
     WScript.Echo oXDoc.selectSingleNode(sXPath).text
  Else
     WScript.Echo oXDoc.parseError.reason
  End If

输出:

123

补充二:

为了展示命名空间的处理原则,我减少/缩短了您的示例:

  Dim sXML   : sXML = Join(Array( _
      "<?xml version=""1.0""?>" _
    , "<NewDataSet>" _
    , "<xs:schema id=""NewDataSet"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"" xmlns="""">" _
    , "<xs:element name=""emp_id"" type=""xs:string"" minOccurs=""0""/>" _
    , "</xs:schema>" _
    , "<ReturningDataSet id=""ReturningDataSet1"">" _
    , "<emp_id>02R349BXC5</emp_id>" _
    , "</ReturningDataSet>" _
    , "</NewDataSet>" _
  ), vbCrLf)
  Dim sNS    : sNS      = "xmlns:xs='http://www.w3.org/2001/XMLSchema'  xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'"
  Dim oXDoc  : Set oXDoc = CreateObject( "Msxml2.DOMDocument.6.0" )
  Dim sXPath : sXPath    = "/NewDataSet/ReturningDataSet/emp_id"
  oXDoc.setProperty "SelectionLanguage", "XPath"
  oXDoc.setProperty "SelectionNamespaces", sNS
  oXDoc.async = False
  oXDoc.loadXml sXML

  If 0 = oXDoc.ParseError Then
     WScript.Echo oXDoc.selectSingleNode(sXPath).text
  Else
     WScript.Echo oXDoc.parseError.reason
  End If

输出:

02R349BXC5
于 2013-03-26T20:04:45.707 回答