如何使用vbscript从XML获取子节点

时间:2014-06-20 08:26:53

标签: xml vbscript xml-parsing

我有以下类型的XML文件:

<ECSC>
   <ATTRIBUTES>
        < all child nodes of attribute tag >
   </ATTRIBUTES>
   <SCRIPT>
      <ETXML_LINE_TABTYPE>
          <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>
    </ETXML_LINE_TABTYPE>
   </SCRIPT>
  <PARAMETERS>
        <all childnodes of parameter of parameters tag >
  </PARAMETERS>
  </ECSC>

我们希望从上面的XML中选择像<SCRIPT>这样的特定节点及其所有子节点,如所有<item>节点;我们想要检测这样的nodeText没有提到任何评论。注释用行表示,它从字符*开始,每个注释行都出现在代码行的正上方;例如,在上面的XML文件中,行<item> SAPGUI ( FB01_100_STEP_2 ).</item>没有任何注释,因此它是一个缺陷,我尝试了以下vbscript。

Dim sFSpec   : sFSpec       = "D:\new Automation\backup\script.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
             msgbox "no comment for:", sItem
           End If
        End If
        bCmnt = False
     End If
 Next
Else
   msgbox objMSXML.parseError.reason
End If

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:2)

&#34;这样的问题选择每个<item>之前没有某种元素类型的元素&#34; 是XPath的理想候选者。

//SCRIPT//item[
  string-length(normalize-space(.)) > 0
  and substring(normalize-space(.), 1, 1) != '*'
  and substring(normalize-space(preceding-sibling::item[1]), 1, 1) != '*'
]

即。

  • <item>下面的任何<SCRIPT>
  • 不是空的
  • 且其第一个非空白字符不是明星
  • 并且其前一个<item>没有明星

在VBScript中:

Option Explicit

Dim XmlDoc
Dim DefectiveLines, Line, LineNum

Set XmlDoc = CreateObject("MSXML2.DOMDocument")

XmlDoc.setProperty "SelectionLanguage", "XPath"
XmlDoc.async = False
XmlDoc.load "D:\new Automation\backup\script.xml"

If XmlDoc.parseError = 0 Then
  Set DefectiveLines = XmlDoc.SelectNodes( _
    "//SCRIPT//item[" & _
      "string-length(normalize-space(.)) > 0" & _
      "and substring(normalize-space(.), 1, 1) != '*'" & _
      "and substring(normalize-space(preceding-sibling::item[1]), 1, 1) != '*'" & _
    "]" _
  )

  For Each Line In DefectiveLines
    LineNum = Line.SelectNodes("./preceding-sibling::item").Length + 1
    WScript.Echo "Error for line " & LineNum & ": " & Line.Text
  Next
Else
   WScript.Echo XmlDoc.parseError.reason
End If

给了我

  • Error for line 1: MESSAGE ( MSG_1 )
  • Error for line 4: SAPGUI ( FB01_100_STEP_2 ).
  • Error for line 25: ENDMESSAGE ( E_MSG_1 ).

请注意,我已经开启了Option Explicit,这是您应该养成的习惯。我还使用了更明智的变量名称。