使用vbscript在XML节点中查找文本

时间:2014-06-26 05:44:43

标签: xml vbscript xml-parsing

我有XML:

<ECSC>
  <SCRIPT>
 <ETXML_LINE_TABTYPE>
                <item>*******************************************************************************.</item>
        <item>* Information.</item>
        <item>********************************************************************************.</item>
        <item>* Script for test case 'TF_FI_FP_FI_0569_MS07_CO_Search_Help_Internal_Orders_vTD0_1_EN.x'</item>
        <item>*</item>
        <item>* For Sub script:</item>
        <item>*  'Test case 3: Choose an Internal Order in One.Fi using external order number while transaction posting (positive case)'.</item>
        <item>*</item>
        <item>* Script is to Display Internal Order using external order number while Transaction Posting 'FB01'</item>
        <item>* GETTAB command is being used to fetch the data from table 'COAS'.</item>
        <item>*</item>
        <item>*</item>
        <item>*     Test data related Information</item>
        <item>*     -----------------------------</item>
        <item>* Default test data present in parameter list has been used while Scripting ( script recording &amp; Performing Checks ).</item>
        <item>*</item>
        <item>* Final execution of result log: 0000037077.</item>
        <item>*</item>
        <item>********************************************************************************.</item>
        <item>* Preparation.</item>
        <item>********************************************************************************.</item>
        <item/>
        <item/>
        <item>********************************************************************************.</item>
        <item>* End Preparation.</item>
        <item>********************************************************************************.</item>
        <item/>
        <item/>
        <item>********************************************************************************.</item>
        <item>* Execution.</item>
        <item>********************************************************************************.</item>
        <item>* To get the 'Table Entries' from table 'COAS'.</item>
        <item>  GETTAB ( COAS , COAS_1 ).</item>
        <item>* To display the value for the field 'External Order No'.</item>
        <item>  LOG ( V_EXTERNAL_ORDER_NO_FRM_TABL ).</item>
        <item/>
        <item>*----------------------Posting(FB01)-------------------------------------------*.</item>
        <item/>
        <item>* This part of Script is to Display Internal Order using external order number while Transaction Posting 'FB01'.</item>
        <item>MESSAGE ( MSG_1 ).</item>

        <item>  GETGUI ( FB01_100_STEP_1 ).</item>

        <item>  SAPGUI ( FB01_100_STEP_2 ).</item>
        <item>* Enter Amount and Tax Code.</item>
        <item>* and, Press F4 help in the Order Field.</item>
        <item>  SAPGUI ( FB01_300_STEP_1 ).</item>
        <item>* In F4 screen, enter the 'External Order Number'</item>
        <item>* pop-up screen is displayed with entries like Order, Description and External Order Number and select 1st order row, press Enter.</item>
        <item>  SAPGUI ( FB01_200_STEP_1 ).</item>
        <item>* To get the values for the field 'Order, Description and External Order No' from F4 help.</item>
        <item>  GETGUI ( FB01_120_STEP_1 ).</item>
        <item>* Press 'Enter' button.</item>
        <item>  SAPGUI ( FB01_120_STEP_3 ).</item>
        <item>* To get the value for the field 'Order' from Main screen.</item>
        <item>  GETGUI ( FB01_300_STEP_2 ).</item>
        <item>* click on 'F3' back button.</item>
        <item>  SAPGUI ( FB01_300_STEP_3 ).</item>
        <item>* click on 'F3' back button.</item>
        <item>  SAPGUI ( FB01_700_STEP_1 ).</item>
        <item>* click 'Yes' button.</item>
        <item>  SAPGUI ( FB01_200_STEP_2 ).</item>
        <item>* click on 'F3' back button.</item>
        <item>  SAPGUI ( FB01_100_STEP_3 ).</item>
        <item>ENDMESSAGE ( E_MSG_1 ).</item>
        <item/>
        <item>* To display the Title Screen.</item>
        <item>  LOG ( V_TITLE_SCREEN ).</item>
        <item>* To display the 'Order' Number from F4 help.</item>
        <item>  LOG ( V_ORDER_NO_FROM_F4 ).</item>
        <item>* To display the 'Description' from F4 help.</item>
        <item>  LOG ( V_DESCRIPTION_FROM_F4).</item>
        <item>* To display the 'External Order no' value from F4 help.</item>
        <item>  LOG ( V_EXTERNAL_ORDER_NO_FROM_F4 ).</item>
        <item>* To display the 'Order' Number from main screen.</item>
        <item>  LOG ( V_ORDER_NO_FRM_MAIN_SCREEN ).</item>
        <item>********************************************************************************.</item>
        <item>* End Execution.</item>
        <item>********************************************************************************.</item>
        <item/>
        <item>********************************************************************************.</item>
        <item>* Check.</item>
        <item>********************************************************************************.</item>
        <item>* To check name of Title screen for transaction FB01.</item>
        <item>  CHEVAR ( V_TITLE_SCREEN = I_TITLE_SCREEN ).</item>
        <item>* To check the value for the field 'External Order No' from F4 help, which should be equal to 'External Order No' from table.</item>
        <item>  CHEVAR ( V_EXTERNAL_ORDER_NO_FRM_TABL = V_EXTERNAL_ORDER_NO_FROM_F4 ).</item>
        <item>* To check the values for the field 'Order' number from Table, which should be equal to 'Order' no from F4 screen and Main screen.</item>
        <item>  CHEVAR ( ( I_ORDER_NUMBER_FROM_TABLE = V_ORDER_NO_FROM_F4 ) AND ( I_ORDER_NUMBER_FROM_TABLE = V_ORDER_NO_FRM_MAIN_SCREEN )).</item>
        <item>********************************************************************************.</item>
        <item>* End Check.</item>
        <item>********************************************************************************.</item>
</ETXML_LINE_TABTYPE>
  </SCRIPT>      
 </ECSC>

从上面的XML文件我想从“MESSAGE”块检查到“ENDMESSAGE”阻止该块是否包含“SAPGUI”之类的语句。如果上面的XML MESSAGE块中不存在单词“SAPGUI”,那么VBscript应该显示错误。 这是我尝试过的VB脚本代码:

 Dim oFS      : Set oFS  = CreateObject("Scripting.FileSystemObject")
 Dim sFSpec   : sFSpec   = oFS.GetAbsolutePathName("D:\new\Link\xmlsample.xml")
 Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
 objMSXML.setProperty "SelectionLanguage", "XPath"
 objMSXML.async = False
 objMSXML.load sFSpec
 sapgui=0
 Set oNodeList2 = objMSXML.documentElement.selectNodes("/ECSC/SCRIPT/ETXML_LINE_TABTYPE/item")
 For Each ndItem In oNodeList2
       Dim sItem : sItem = ndItem.text
       If (Left(sItem, 7)="MESSAGE") Then
      If (Left(sItem, 8)="  SAPGUI") Then
        sapgui=sapgui+1
        msgbox("SAPGUI is present")
      else
            if(sapgui = 0) Then
                msgbox("SAPGUI code is not present")
        End if
      End If
  End If
 Next

请帮帮我。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您可以更好地利用XPath来进行所需的匹配。我使用了您提供的完全相同的XML示例,并提出了这个:

Option Explicit

Dim xml : Set xml = CreateObject("MSXML2.DOMDocument.6.0")
Call xml.SetProperty("SelectionLanguage", "XPath")
xml.Async = False
Call xml.Load("test.xml")

Dim nl : Set nl = xml.documentElement.selectNodes("//ETXML_LINE_TABTYPE/item[text()='MESSAGE.']/../item[contains(.,'SAPGUI')]")

If nl Is Nothing Or nl.Length < 1 Then
  Call MsgBox("Error", vbCritical, "Error")
End If

WScript.Quit

如果&#39;项目&#39;将会出错。包含文字&#39; MESSAGE的节点。&#39;没有兄弟姐妹&#39;项目&#39;包含文本&#39; SAPGUI&#39;

的节点

答案 1 :(得分:0)

你可以尝试这样的方法来检查SAPGUIMESSAGE之间是否有字符ENDMESSAGE

Set oNodeList = objMSXML.selectSingleNode("//ETXML_LINE_TABTYPE").selectNodes("//item")

ReDim items(oNodeList.Length - 1)
i = 0
For Each node In oNodeList
  items(i) = node.text
  i = i + 1
Next

txt = Join(items, vbLf)

Set msg = New RegExp
msg.Pattern = "MESSAGE([\s\S]*?)ENDMESSAGE"

For Each m In re.Execute(txt)
  If InStr(m.SubMatches(0), "SAPGUI") > 0 Then
    WScript.Echo "Message contains SAPGUI."
  Else
    WScript.Echo "Message does not contain SAPGUI."
  End If
Next

可能不是最优雅的方式,但输入数据的格式也是如此。