【问题标题】:How to read different multiple nodes from xml using vbscript如何使用 vbscript 从 xml 读取不同的多个节点
【发布时间】:2014-06-06 09:49:33
【问题描述】:

当我在不同的 xml 文件中分离“ATTRIBUTE”和“SCRIPT”xml 节点时,我能够成功读取文件。但是当我将这两个节点组合在单个 xml 文件中时,我无法读取 xml 文件。所以请帮助我。非常感谢您的帮助。

这是我的 xml 文件

<ATTRIBUTES>
    <ETTOOLNAME>ECATT</ETTOOLNAME>
    <ETOBJ_GNDT>
        <VERSION>00000001</VERSION>
           <TWB_TITLE>TF_FI_FP_FI_0569_MS07_CO_Search_Help_Internal_Orders_vTD0_1_EN.x</TWB_TITLE>
        <TWB_STATUS>X</TWB_STATUS>
        <TWB_RELE>N</TWB_RELE>
        <FUSER>ECATT</FUSER>
        <FDATE>2014-05-22</FDATE>
        <LUSER>ECATT</LUSER>
        <LDATE>2014-05-22</LDATE>
        <LTIME>13:59:50</LTIME>
    </ETOBJ_GNDT>
    <ETOBJNOVER>
        <NAME>ZX_FI_FP_0569_MS07_COAS_FB01</NAME>
        <TYPE>ECSC</TYPE>
        <TWB_RESP>ECATT</TWB_RESP>
        <TWB_DISTL>B</TWB_DISTL>
        <DEVCLASS>Z_SOL_ONEERP</DEVCLASS>
        <MASTERLANG>E</MASTERLANG>
        <TADIR_RESP>ECATT</TADIR_RESP>
        <FRANGE>BC</FRANGE>
    </ETOBJNOVER>
    <ETOBJ_DOC>
        <SEARCH_1>FI_FP_FI_0569_MS07</SEARCH_1>
        <SEARCH_2>COAS</SEARCH_2>
        <SEARCH_3>KO03</SEARCH_3>
    </ETOBJ_DOC>
    <ETOBJ_CNST>
        <TWB_WKREQ>0.000</TWB_WKREQ>
        <TWB_PRIO>3</TWB_PRIO>
    </ETOBJ_CNST>
    <ETSC_TSYS>
        <SYSTEMDATA>Z_SD_1ERP_Z</SYSTEMDATA>
        <TESTSYSTEM>FI_TRUSTED_EN</TESTSYSTEM>
    </ETSC_TSYS>
    <ETSYS_COMP_TABTYPE/>
    <ETSYS_REL_TABTYPE/>
</ATTRIBUTES>


  <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>* To get the name of the Title Screen.</item>
        <item>  GETGUI ( FB01_100_STEP_1 ).</item>
        <item>* Enter the Required details and Press Enter.</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>

我写的vbscript如下:

 Const XMLDataFile = "C:\temp\new\output.xml"

  Set xmlDoc = CreateObject("Microsoft.XMLDOM")
  xmlDoc.Async = False
  xmlDoc.Load(XMLDataFile)
  xmlDoc.validateOnParse=False

  strQuery1 = "/ATTRIBUTES/ETOBJ_GNDT/( VERSION | TWB_TITLE | TWB_STATUS | FUSER | FDATE | LUSER | LDATE )"

  Set colNodes = xmlDoc.selectNodes( strQuery1 )
  For Each objNode in colNodes
if(objNode.nodeName="VERSION") Then
Document.write("VERSION :  " & objNode.text & "<br><br>")
    'Document.write(objNode.nodeName & " :  " & objNode.text & "<br><br>")
End if

if(objNode.nodeName="TWB_TITLE") Then
    Document.write("SCRIPT TITLE :  " & objNode.text & "<br><br>")
End if

if(objNode.nodeName="TWB_STATUS") Then
    Document.write("SCRIPT STATUS :  " & objNode.text & "<br><br>")
End if

if(objNode.nodeName="FUSER") Then
    Document.write("CREATED BY :  " & objNode.text & "<br><br>")
End if

if(objNode.nodeName="FDATE") Then
    Document.write("CREATED ON :  " & objNode.text & "<br><br>")
End if

if(objNode.nodeName="LUSER") Then
    Document.write("CHANGED BY :  " & objNode.text & "<br><br>")
End if

if(objNode.nodeName="LDATE") Then
    Document.write("CHANGED ON :  " & objNode.text & "<br><br>")
End if

 Next

 strQuery2 = "/ATTRIBUTES/ETOBJNOVER/(NAME|TWB_RESP|DEVCLASS|FRANGE )"

 Set colNodes = xmlDoc.selectNodes( strQuery2 )

 For Each objNode in colNodes
 'Document.write(objNode.nodeName & " :  " & objNode.text & "<br><br>")

 if(objNode.nodeName="NAME") Then

   Document.write("SCRIPT NAME :  " & objNode.text & "<br><br>")

End if
if(objNode.nodeName="TWB_RESP") Then
    Document.write("PERSON RESPONSIBLE :  " & objNode.text & "<br><br>")
End if
if(objNode.nodeName="DEVCLASS") Then
    Document.write("PACAKGE :  " & objNode.text & "<br><br>")
End if
if(objNode.nodeName="FRANGE") Then
    Document.write("SOFTWARE COMPONENT :  " & objNode.text & "<br><br>")
End if

Next

strQuery3 = "/ATTRIBUTES/ETOBJ_DOC/(SEARCH_1|SEARCH_2 )"

Set colNodes = xmlDoc.selectNodes( strQuery3 )
For Each objNode in colNodes
'Document.write(objNode.nodeName & " :  " & objNode.text & "<br><br>")

if(objNode.nodeName="SEARCH_1") Then
    Document.write("SEARCH TERM 1 :  " & objNode.text & "<br><br>")
End if
if(objNode.nodeName="SEARCH_2") Then
    Document.write("SEARCH TERM 2 :  " & objNode.text & "<br><br>")
End if

 Next

strQuery4 = "/ATTRIBUTES/ETSC_TSYS/(SYSTEMDATA|TESTSYSTEM)"

Set colNodes = xmlDoc.selectNodes( strQuery4 )
For Each objNode in colNodes
'Document.write(objNode.nodeName & " :  " & objNode.text & "<br><br>")

if(objNode.nodeName="SYSTEMDATA") Then
    Document.write("SYSTEM DATA CONTAINER :  " & objNode.text & "<br><br>")
End if
if(objNode.nodeName="TESTSYSTEM") Then
    Document.write("TARGET SYSTEM :  " & objNode.text & "<br><br>")
End if


Next

Set objNodeList = xmlDoc.getElementsByTagName("SCRIPT") 

strValues = ""
If objNodeList.length > 0 then 
    For each x in objNodeList 
        If strValues = "" Then
            strValues = x.Text
        Else
            strValues = strValues & ";" & x.Text
        End If
    Next 
Else 
    strValues = ""
End If
If strValues = "" Then
    Document.write("No child elements found.")
Else
    arrValues = Split(strValues, ";")
    Document.write(Join(arrValues, VbCrLf))
End If

我只想要那些标签名称为&lt;item&gt;的标签值
我怎样才能得到它..?

【问题讨论】:

    标签: xml vbscript


    【解决方案1】:

    您确实应该使用以下框架脚本开始所有 XML 工作:

      Dim oFS      : Set oFS      = CreateObject("Scripting.FileSystemObject")
      Dim sFSpec   : sFSpec       = oFS.GetAbsolutePathName("..\testdata\xml\24078916-1.xml")
      Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
      objMSXML.setProperty "SelectionLanguage", "XPath"
      objMSXML.async = False
      objMSXML.load sFSpec
    
      If 0 = objMSXML.parseError Then
         ' do something sensible here
      Else
         WScript.Echo objMSXML.parseError.reason
      End If
    

    进行最小的错误检查(并避免像虚假()这样的暴行)。将此应用于您发布的 .XML:

    Only one top level element is allowed in an XML document.
    

    当我将您的 .XML 修改为

    <root>
      ... your content ...
    </root>
    

    骨架脚本可以很好地加载 .XML。

    【讨论】:

    • 您好,我已根据您的建议更新了我的代码,但它给出了诸如“XML 文档中只允许一个顶级元素”之类的错误。这是什么意思..?我不能显示/读取两个以上的不同节点..?请帮帮我..
    • @Pravin - 如果您不理解错误信息,请在此处开始您的研究:w3.org/standards/xml
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 1970-01-01
    相关资源
    最近更新 更多