Tuesday, September 15, 2009

Get value in XML Node with XPath (Node Group)

Below code we can Get multiple values with Node path from XML file.
Set xmlDoc = CreateObject ( "Microsoft.XMLDOM")
xmlDoc.Async = "false" xmlDoc.Load ( "C:\Test1.xml")
i=0
For Each personneElement In xmlDoc.selectNodes ( "/Modules/Module/Options/Option/Skus/Sku/Price/AssociatedPrices/Price/AssociatedPrices/Price/Type")

If personneElement.text = "Expectedvalue" Then
MsgBox personneElement.text
' we can get Two previous node value with below statement
MsgBox personneElement.PreviousSibling.PreviousSibling.text
' personneElement. i= i+1
End If
Next
msgbox i

Reference :-
http://techdos.com/content/view/77/74/

Monday, September 14, 2009

XML file Conversion from ANSI to Unicode

Some XML files which are saved in ANSI Encoding type are not fully open in IE browser. Those files we have to save as in Unicode Encoding Type then we can open those files without errors. We can do above action in QTP with below function.

Function convertANSItoUTF8(input_file,output_file)
Const adTypeBinary = 1
Const adTypeText = 2
Const bOverwrite = True
Const bAsASCII = False
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFFSpec : sFFSpec = oFS.GetAbsolutePathName( input_file )
Dim sTFSpec : sTFSpec = oFS.GetAbsolutePathName(output_file )
Dim oFrom : Set oFrom = CreateObject( "ADODB.Stream" )
Dim sFrom : sFrom = "Windows-1252"
Dim oTo : Set oTo = CreateObject( "ADODB.Stream" )
Dim sTo : sTo = "utf-8"
If oFS.FileExists( sTFSpec ) Then oFS.DeleteFile sTFSpec
oFrom.Type = adTypeText
oFrom.Charset = sFrom
oFrom.Open
oFrom.LoadFromFile sFFSpec
msgbox oFrom.Size & " Bytes in " & sFFSpec
oTo.Type = adTypeText
oTo.Charset = sTo
oTo.Open
oTo.WriteText oFrom.ReadText
msgbox oTo.Size & " Bytes in " & sTFSpec
oTo.SaveToFile sTFSpec
oFrom.Close oTo.Close
End Function

Another Way:-

Const ForReading = 1, ForWriting = 2
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set fso = CreateObject("Scripting.FileSystemObject")
XMLPath= "C:\File.xml"
fso.CreateTextFile XMLPath ' Create a file.
Set f = fso.GetFile(XMLPath)
Set ts = f.OpenAsTextStream(ForWriting, TristateTrue)
ts.Write Browser("Browser").Page("Page").WebXML("ObjectName").GetData
ts.Close