XML parsing code
Option Explicit Dim Path As String ' input path name Dim FileName As String ' input file name Dim intColumnCount As Integer ' column counter Dim intLoop As Integer ' Looping integer Dim objDictionary As Scripting.Dictionary ' dictionary object to store column identification for id, method, query string etc Dim intPrevRequest_id As Integer 'stores previous request id Dim intCurrRequest_id As Integer 'stores current request id Dim strWholeReq As String ' Full request that is ready to be written to file Dim strStartQuotes As String ' Placeholder which holds starting double quotes Dim strEndQuotes As String ' Placeholder which holds ending double quotes Dim strStepName As String ' First line of the Parsed_XML_Function. eg Parsed_XML_Function("Step5", 'Here 5 comes from intStepNum variable Dim strUrl As String ' contains URL and Query string Dim strQueryStr As String ' Query string Dim strMethod As String ' Method part of request Dim strBody As String 'Body attributes Dim strMisc As String ' Misc items such as Resource, Snapshot number etc Dim strContentType As String ' Content type of request Dim intStepNum As Integer ' iterative count to identify step Dim objFileSys As Scripting.FileSystemObject ' file system object Dim objFile As Scripting.File 'file object Dim objTextStr As Scripting.TextStream 'text stream object Dim ActionFileName As String ' destination action name 'this funciton is the main function which calls other functions Sub Main() Path = Worksheets(1).Cells(1, 2).Value FileName = Worksheets(1).Cells(2, 2).Value ActionFileName = Worksheets(1).Cells(3, 2).Value 'open xml file Workbooks.Open FileName:=Path & "\" & FileName 'activate the workbook Windows(FileName).Activate 'delete first row Rows("1:1").Select Selection.Delete Shift:=xlUp Range(Selection, Selection.End(xlToRight)).Select ActiveSheet.Name = "PARSINGVS_XML" 'get total columns and analyze the columns intColumnCount = Worksheets("PARSINGVS_XML").UsedRange.Columns.Count Set objDictionary = New Dictionary intLoop = 1 For intLoop = 1 To intColumnCount If InStr(1, Worksheets("PARSINGVS_XML").Cells(1, intLoop), "Request/#id", 1) > 0 Then objDictionary.Add "Req_id", intLoop ElseIf InStr(1, Worksheets("PARSINGVS_XML").Cells(1, intLoop), "Request/@Method", 1) > 0 Then objDictionary.Add "Req_method", intLoop ElseIf InStr(1, Worksheets("PARSINGVS_XML").Cells(1, intLoop), "Request/@Url", 1) > 0 Then objDictionary.Add "Req_url", intLoop ElseIf InStr(1, Worksheets("PARSINGVS_XML").Cells(1, intLoop), "FormPostHttpBody/@ContentType", 1) > 0 Then objDictionary.Add "Req_contenttype", intLoop ElseIf InStr(1, Worksheets("PARSINGVS_XML").Cells(1, intLoop), "FormPostParameter/@Name", 1) > 0 Then objDictionary.Add "Req_itemdata_name", intLoop ElseIf InStr(1, Worksheets("PARSINGVS_XML").Cells(1, intLoop), "FormPostParameter/@Value", 1) > 0 Then objDictionary.Add "Req_itemdata_value", intLoop ElseIf InStr(1, Worksheets("PARSINGVS_XML").Cells(1, intLoop), "QueryStringParameter/@Name", 1) > 0 Then objDictionary.Add "Req_querystring_name", intLoop ElseIf InStr(1, Worksheets("PARSINGVS_XML").Cells(1, intLoop), "QueryStringParameter/@Value", 1) > 0 Then objDictionary.Add "Req_querystring_value", intLoop End If Next 'Loop through all requests and capture querysting, itemdata, url, method, action and content type '----------------------------------------------- 'Initialize variables ot default value at start '----------------------------------------------- intPrevRequest_id = 1 intCurrRequest_id = 1 strStartQuotes = """" strEndQuotes = """," & vbCrLf intStepNum = 1 strQueryStr = "" strBody = "" Set objFileSys = New Scripting.FileSystemObject objFileSys.CreateTextFile (Path & "\" & ActionFileName) Set objFile = objFileSys.GetFile(Path & "\" & ActionFileName) Set objTextStr = objFile.OpenAsTextStream(ForAppending, TristateUseDefault) intLoop = 2 'first line is the header For intLoop = 2 To Worksheets("PARSINGVS_XML").UsedRange.Rows.Count If objDictionary.Exists("Req_id") Then intCurrRequest_id = Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_id")).Value) Else MsgBox "XML do nto contain Request id column" Exit Sub End If 'if current and previous request id are not same OR we are at end of steps the write to file If (intPrevRequest_id <> intCurrRequest_id) Or (intLoop = Worksheets("PARSINGVS_XML").UsedRange.Rows.Count) Then Call WriteToFile 'iterate to next step intStepNum = intStepNum + 1 strQueryStr = "" strBody = "" intPrevRequest_id = intCurrRequest_id End If Call Write_Remaining_DESTINATIONVS_Req ' build the DESTINATIONVS request apart from Body & Query string Call WriteQuery_Body 'build hte body and querystring Next MsgBox "Completed" Set objDictionary = Nothing objTextStr.Close Set objTextStr = Nothing Set objFile = Nothing Set objFileSys = Nothing Windows(FileName).Close (False) End Sub 'funciton to write contents to file Sub WriteToFile() strWholeReq = strWholeReq & vbCrLf & strStepName & strUrl If strQueryStr <> "" Then strWholeReq = strWholeReq & "?" & strQueryStr End If strWholeReq = strWholeReq & strEndQuotes & strMethod & strContentType & strMisc If strBody <> "" Then strWholeReq = strWholeReq & strStartQuotes & "Body=" & strBody & strEndQuotes End If strWholeReq = strWholeReq & " LAST);" & vbCrLf objTextStr.WriteLine strWholeReq strWholeReq = "" End Sub 'function to build the querystring and body part which are iterative Sub WriteQuery_Body() If objDictionary.Exists("Req_querystring_name") Then If Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_querystring_name")).Value) <> "" Then If strQueryStr <> "" Then strQueryStr = strQueryStr & "&" End If 'Querystring strQueryStr = strQueryStr & Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_querystring_name")).Value) & "=" & _ Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_querystring_value")).Value) End If End If If objDictionary.Exists("Req_itemdata_name") Then If Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_itemdata_name")).Value) <> "" Then If strBody <> "" Then strBody = strBody & "&" End If 'Body strBody = strBody & Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_itemdata_name")).Value) & "=" & _ Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_itemdata_value")).Value) End If End If End Sub 'function which creates remaining part of web_custom request other than querystring and body Sub Write_Remaining_DESTINATIONVS_Req() 'Name of Parsed_XML_Function("Step2", strStepName = "Parsed_XML_Function(" & strStartQuotes & "Step" & intStepNum & strEndQuotes If objDictionary.Exists("Req_url") Then '"URL = " strUrl = strStartQuotes & _ "URL=" & Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_url")).Value) End If If objDictionary.Exists("Req_method") Then 'Method = strMethod = strStartQuotes & _ "Method=" & Trim(Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_method")).Value)) & strEndQuotes End If If objDictionary.Exists("Req_contenttype") Then 'ContentType = If Trim(Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_contenttype")).Value)) <> "" Then strContentType = strStartQuotes & _ "RecContentType=" & Trim(Trim(Worksheets("PARSINGVS_XML").Cells(intLoop, objDictionary("Req_contenttype")).Value)) & strEndQuotes Else strContentType = strStartQuotes & "RecContentType=text/html" & strEndQuotes End If Else strContentType = strStartQuotes & "RecContentType=text/html" & strEndQuotes End If 'remaining all strMisc = strStartQuotes & "TargetFrame=" & strEndQuotes & _ strStartQuotes & "Resource=0" & strEndQuotes & _ strStartQuotes & "Referer=" & strEndQuotes & _ strStartQuotes & "Mode=HTML" & strEndQuotes & _ strStartQuotes & "Snapshot=t" & intStepNum & ".inf" & strEndQuotes End Sub