使用vbscript进行xml验证

时间:2014-06-19 17:58:13

标签: xml vbscript xml-parsing

我想使用vbscript验证xml以下是我的示例xml文件。

<SCRIPT>
     <item>MESSAGE ( MSG_1 ).</item>
        <item>* To get the name of the Title Screen.</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>

</SCRIPT>

我们希望检测到这样的nodeText没有提到任何评论。注释由从字符*开始的行表示,并且每个注释行位于代码行的正上方。例如:在上面的XML文件<item> SAPGUI ( FB01_100_STEP_2 ).</item>中,此代码行没有任何注释,因此它是缺陷的。我尝试了以下vbscript。

Const XMLDataFile = "E:\automation\vittsnew\script.xml"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = False
xmlDoc.Load(XMLDataFile)
    xmlDoc.validateOnParse = True  

    If xmlDoc.Load(XMLDataFile) Then 
         Msgbox "SUCCESS loading XML File"  
    Else  
        Msgbox "ERROR loading XML File"  
    End If

    f1=0
    Set root = xmlDoc.documentElement
    Set items = root.childNodes
    for each item in items
         strLine = xmlDoc.getElementsByTagName("*")
         if (strLine ="MESSAGE ( MSG_1 ).") then
         msgbox "Inside MESSAGE"
        If (Left(strLine, 1) = "*") Then
         if((Right(strLine,1))=".") then 
                    Msgbox("ERROR Found  at Line No :" & strLine)
        End If
        fl=1
        else 
                if(((Right(strLine,1))=".") and (fl=1))Then
                      fl=0
                else
                        Msgbox("ERROR Found  at Line No :" & strLine)
                end if
             end if
    end if       
    next

请帮助我......提前谢谢你。

1 个答案:

答案 0 :(得分:1)

对于XML,&#34;验证&#34;和.validateOnParse指的是针对模式文件检查.XML。您的问题是纯结构化文本检查的子类型:每个非注释行都是注释。所以你需要一个带有状态的行循环来保存前面的行是否是注释。在代码中:

  Dim sFSpec   : sFSpec       = "..\data\24313332.xml"
  Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
  objMSXML.setProperty "SelectionLanguage", "XPath"
  objMSXML.async = False
  objMSXML.load sFSpec

  If 0 = objMSXML.parseError Then
     Dim bCmnt : bCmnt = False
     Dim ndItem
     For Each ndItem In objMSXML.documentElement.childNodes
         Dim sItem : sItem = ndItem.text
         If "*" = Left(sItem, 1) Then
            bCmnt = True
         Else
            If Not bCmnt Then
               If "" <> sItem And 0 = Instr(sItem, "MESSAGE") Then
                  WScript.Echo "no comment for:", sItem
               End If
            End If
            bCmnt = False
         End If
     Next
  Else
     WScript.Echo objMSXML.parseError.reason
  End If

输出:

no comment for: SAPGUI ( FB01_100_STEP_2 ).