我有一个带有函数的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><?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="errorSev" type="xs:string" minOccurs="0"/><xs:element name="errorId" type="xs:string" minOccurs="0"/><xs:element name="errorText" type="xs:string" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><ReturningDataSet id="ReturningDataSet1" rowOrder="0"><errorSev>20</errorSev><errorId>CDA1356</errorId><errorText>FDWarning1: Freq of interest Payment not available from prefered data source FREQ_RATE_CHANGE_CD. Data system defaulted. </errorText></ReturningDataSet></NewDataSet></warningMessages>
</ReturningDataSet>
</NewDataSet>
答案 0 :(得分: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
已添加II:
为了显示原则上对命名空间的处理,我减少/缩短了你的样本:
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