【问题标题】:How to get child nodes from XML using vbscript如何使用 vbscript 从 XML 获取子节点
【发布时间】:2014-06-20 08:26:53
【问题描述】:

我有以下类型的 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 中选择一个特定的节点,如 &lt;SCRIPT&gt; 及其所有子节点,如所有 &lt;item&gt; 节点;我们想检测这样的 nodeText 没有提到任何评论。注释由从字符* 开始的行表示,并且每个注释行都出现在代码行的正上方;例如在上面的 XML 文件中,行:&lt;item&gt; SAPGUI ( FB01_100_STEP_2 ).&lt;/item&gt; 没有任何注释,所以这是一个缺陷,我尝试了以下 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

请帮助我。提前谢谢你。

【问题讨论】:

  • “我们想要检测这样的nodeText没有提到任何评论是什么意思。” ?这很令人困惑。

标签: xml vbscript xml-parsing


【解决方案1】:

“选择前面没有特定类型元素的每个&lt;item&gt;”这样的问题是 XPath 的完美候选。

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

  • &lt;SCRIPT&gt; 下方的任何&lt;item&gt;
  • 不为空
  • 并且其第一个非空白字符不是星号
  • 其前面的&lt;item&gt; 也没有星号

在 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,您应该养成这样做的习惯。我还使用了更合理的变量名。

【讨论】:

  • 是的,现在它工作得很好......非常感谢你的朋友......谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多