preload

Generating text file from XML template (QTP, VBScript)

Posted by Albert Gareev on Dec 09, 2009 | Categories: Source codeText DataXML Data


Parent page: Service Functions – System (QTP, VBScript)

Description

If you need to generate text file with values you don’t know ahead you may do it dynamically, by utilizing the function below. Make sure you designed template first.
The current logic is oriented to executable text files generation, like Dexterity Macro or OS Shell batch files. Code lines are data-driven, if data not supplemented then the line will be omitted.
You may change the logic and extend the functionality as you need.

Function Libraries

Service Functions – XML (QTP, VBScript)

Service Functions – String (QTP, VBScript)

Generate Text File


Public Function GenerateTextFile(ByVal sXMLPattern, ByVal sOutputFile)
Dim boolRC
Dim FSO
Dim objXMLPattern, objRoot
Dim objOptions, sDefaultWrap
Dim objTextLines, objTextLine, objDataSet, objDataNode
Dim objTextFile
Dim Iter, sType, sText, boolDatadriven, boolActiveLine, sWrap
Dim Jter, sToken, sValue, sLine

'Verify source files exist
Set FSO = CreateObject("Scripting.FileSystemObject")
boolRC = FSO.FileExists(sXMLPattern)

If Not boolRC Then
GenerateTextFile = FALSE
Set FSO = Nothing
Exit Function
End If

'Open XML pattern
Set objXMLPattern = XMLUtil.CreateXMLFromFile(sXMLPattern)
Set objRoot = objXMLPattern.GetRootElement()

'Get options node
Set objOptions = ChildElementByName(objRoot, "Options")
sDefaultWrap = ChildAttributeValueByName(objOptions, "wrap")
Set objTextLines = objRoot.ChildElementsByPath("./Line")

'Rewrite output file
Set objTextFile = FSO.CreateTextFile(sOutputFile, TRUE)

'Generate and write text lines in loop
For Iter=1 To objTextLines.Count

Set objTextLine = objTextLines.Item(Iter)
sType = ChildAttributeValueByName(objTextLine, "type")
sText = ChildAttributeValueByName(objTextLine, "text")

Select Case LCase(sType)

Case "comment"
objTextFile.WriteLine(sText)

Case "code"

boolDatadriven = InitBool(ChildAttributeValueByName(objTextLine, "data-driven"), FALSE)
If boolDatadriven Then
Set objDataSet = objTextLine.ChildElementsByPath("./Data")
boolActiveLine = TRUE
sLine = sText
'Generate text line
For Jter=1 To objDataSet.Count
Set objDataNode = objDataSet.Item(Jter)
'Parameter name
sToken = Trim(ChildAttributeValueByName(objDataNode, "name"))
If sToken = "" Then
boolActiveLine = FALSE
Exit For
End If
'Data value

'Note. You may use any Data Model here: Array, Dictionary, DataTable object, Environment object, etc.
sValue = ' - data source according to your data model - provide value based on token retrieved

If sValue = "" Then
boolActiveLine = FALSE
Exit For
End If

'wrapping chars
sWrap = ChildAttributeValueByName(objDataNode, "wrap")
If sWrap = "" Then
sWrap = sDefaultWrap
End If

'Substitute data
sLine = Replace(sLine, "%"&sToken, sWrap & sValue & sWrap)
Next

'Save new line if applicable
If boolActiveLine Then
objTextFile.WriteLine(sLine)
End If
Else
objTextFile.WriteLine(sText)
End If

Case Else
'ignore. do nothing
End Select

Next

objTextFile.Close()
Set FSO = Nothing
Set objXMLPattern = Nothing

GenerateTextFile = TRUE
End Function


  • One response to "Generating text file from XML template (QTP, VBScript)"

  • Anonymous
    10th February 2010 at 11:53

    How can I get FSO to read from xml file?

    [ Albert’s Reply: you can read it as a text file but you won’t get XML DOM created. ]

Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by Albert Gareev is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.