Compare two xml files using VBS in QTP - vbscript

I need to compare 2 xml files using QTP where the values for each tag needs to be compared and need to print the difference in values if found. I used the built in Function XMLUTIL but its not working as expected i.e.. its creates a file with differences including the parent tag.
<tns:AAL_Request_NEW xsi:schemaLocation="http://www.bnymellon.com/AAL_Request_NEW AAL_Request_NEW.xsd">
<tns:OPF_Information>
<tns:Source>
<tns:Source>EPH</tns:Source>
</tns:Source>
<tns:References>
<tns:Accounting_Record_Reference>130830000672401</tns:Accounting_Record_Reference>
<tns:OPF_Reference>EPH1308300006724</tns:OPF_Reference>
<tns:Group_Reference>EPH1308300006723</tns:Group_Reference>
</tns:References>
</tns:OPF_Information>
</tns:AAL_Request_NEW>
In the above xml file i just need the tags with values like
tns:Source with value EPH, tns:Accounting_Record_Reference with value 130830000672401, tns:OPF_Reference with value EPH1308300006724 and tns:Group_Reference EPH1308300006723 to be compared and not the parent tags like tns:References, tns:OPF_Information or tns:AAL_Request_NEW.
Can anyone help with the logic to fetch the tags which has no child tag inside it and ends immediately with having only a value between its start <> and end and compare it with the other file and print the tag name and the values if there is a difference?

you can use CreateObject("Microsoft.XMLDOM") to read the xml files and retrive the tags by tag name and comparte them both.
Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("<XML PATH>")
Set Root = objXMLDoc.documentElement
Set tags = root.tagnames
Set NodeList = Root.getElementsByTagname("<node name>")
For Each Elem In NodeList
msgbox Elem.text
Next
Thanks and regards

Related

UFT API -- "Select data" How to return values for query to Sybase database

I have to validate an API output with a values stored in Sybase DB.
When I use "select Data" activity to fetch the value from Sybase table, the value is returned in XML format as below:
<Row>
<ColumnName1> Value </ColumnName1>
<ColumnName2> Value </ColumnName2>
<ColumnName3> Value </ColumnName3>
</Row>
However I need only the Value so I can save it in excel to compare in my next step. Comparison is being done on value basis not with xml. I need to convert the response from select data (which is in XML format shown above) .
The subsequent checkpoint in API output fails as I need to provide individual values as input to the service.
I will need a solution that can either split array and give me individual values at run time Or I should be able to save values in Excel which I can save and import at run time.
You can use the COM object for Microsoft's XML Parser. Something like this should do it,
set objXml = CreateObject("Microsoft.XMLDOM")
objXml.Load("C:\yourxmlpath.xml")
' Get your node (you can traverse it like a path)
set allMathchingRows = objXml.selectNodes("/Row")
For intRow = 0 To allMathchingRows.Length - 1
For each Node in allMathchingRows(intRow).ChildNodes
strAllRowData = strAllRowData + "," + Node.text
Next
' Remove the ' appended to the start of string
strAllRowData = Right(strAllRowData, Len(strAllRowData) - 1)
Next
you may want to refine it more if you want your data separate for each rows

Dynamically Rename Shared OR object names

As per the naming convention we are following, we need to rename every object to its standard name.
One such convention is to replace space between with ‘_’
eg. Object name ->Object_name
Is there any way to perform it dynamically using lines of code.?
What you can do , Export the repository to the XML . Then Using the XML dom Object you can navigate to each Node.Each Node will have a Name Attribute .Then you can check there is a space if it is You can change the logical name of it .This will change the Object Repository Names .
But you need do similar king of change in your QTPscript to get reflected .
Export the OR to xml file and use the following line of code.
And use the xml generated to import OR back to QTP.
This is specific to SAP GUI
Function ModifyORXML(inputFilepath,outputFilepath)
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async = "False"
xmlDoc.Load(inputFilepath)
Set xmlNodeList = xmlDoc.getElementsByTagName("qtpRep:Object")
num = xmlNodeList.length
For each x in xmlNodeList
AttName=x.getattribute("Name")
If x.getattribute("Class")="SAPGuiButton" Then
tmp=Split(AttName," ",-1,1)
AttName=tmp(0)
End If
AttName=Replace(AttName,Chr(34)," ")
AttName=Replace(AttName,")"," ")
AttName=Trim(AttName)
oldAttName=AttName
AttName=Replace(AttName,":"," ")
AttName=Trim(AttName)
AttName=Replace(AttName," ","_")
AttName=Replace(AttName," __","_",1,-1,1)
x.Attributes.getNamedItem("Name").Text = AttName
Next
xmlDoc.Save outputFilepath
End Function

VBScript Find a node in XML node and replace the value

How to write a vbscript which should search for a specific node in a XML file and replace the value of that node with another value?
So far I can read a node and get the value.
set objXML = CreateObject("Microsoft.XMLDOM")
objXML.async = "false"
objXML.load("E:\sage2\test.xml")
Set Root = objXML.documentElement
For Each x In Root.childNodes
if x.nodename="showList" then
plot=x.text
msgbox plot
end if
Next
please suggest me some example which should read a specific node in the xml file and replace the value of that node.
Here is a simple XML edit and save example in VBScript. I recommend lokking into using Xpath to select your node instead of looping over the child nodes, you can provide your XML for more detailed answer.
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.load "MYFILE.xml"
'Locate the desired node
'Note the use of XPATH instead of looping over all the child nodes
Set nNode = xmlDoc.selectsinglenode ("//parentnode/targetnode")
'Set the node text with the new value
nNode.text = "NEW VALUE"
'Save the xml document with the new settings.
strResult = xmldoc.save("MYFILE.xml")

MSXML2.DOMDocument.xml give me malformed xml

We have an old legacy system that where a component is writter in VB6. One method returns a string that is xml data. The xml data is created with msxml3.dll MSXML2.DOMDocument and returns the data of the document with the property xml: http://msdn.microsoft.com/en-us/library/ms755989(v=VS.85).aspx
However, some data of the xmldocument is from the database and one field is a hashed password string. The code that set the data for the element:
Set cellNode = rowNode.appendChild(xml.createElement("COL"))
If IsNull(rs(oField.name).Value) Then
cellNode.Text = ""
Else
cellNode.Text = rs(oField.name).Value
End If
This gives me malformed/non-wellformed xml:
<ROWS><ROW><COL>r<í</COL></ROW></ROWS>
Is there a workaround for this?
You should escape unicode characters. Or put them in a CDATA tag (which is not such a nice solution though)
Btw < > and & should be escaped as well.

Iterate over Umbraco getAllTagsInGroup result

I'm trying to get a list of tags from a particular tag group in Umbraco (v4.0.2.1) using the following code:
var tags = umbraco.editorControls.tags.library.getAllTagsInGroup("document downloads");
What I want to do is just output a list of those tags. However, if I output the variable 'tags' it just outputs a list of all tags in a string. I want to split each tag onto a new line.
When I check the datatype of the 'tags' variable:
string tagType = tags.GetType().ToString();
...it outputs MS.Internal.Xml.XPath.XPathSelectionIterator.
So question is, how do I get the individual tags out of the 'tags' variable? How do I work with a variable of this data type? I can find examples of how to do it by loading an actual XML file, but I don't have an actual XML file - just the 'tags' variable to work with.
Thanks very much for any help!
EDIT1: I guess what I'm asking is, how do I loop through the nodes returned by an XPathSelectionIterator data type?
EDIT2: I've found this code, which almost does what I need:
XPathDocument document = new XPathDocument("file.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("/tags/tag");
nodes.MoveNext();
XPathNavigator nodesNavigator = nodes.Current;
XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);
while (nodesText.MoveNext())
debugString += nodesText.Current.Value.ToString();
...but it expects the URL of an actual XML file to load into the first line. My XML file is essentially the 'tags' variable, not an actual XML file. So when I replace:
XPathDocument document = new XPathDocument("file.xml");
...with:
XPathDocument document = new XPathDocument(tags);
...it just errors.
Since it is an Iterator, I would suggest you iterate it. ;-)
var tags = umbraco.editorControls.tags.library.getAllTagsInGroup("document downloads");
foreach (XPathNavigator tag in tags) {
// handle current tag
}
I think this does the trick a little better.
The problem is that getAllTagsInGroup returns the container for all tags, you need to get its children.
foreach( var tag in umbraco.editorControls.tags.library.getAllTagsInGroup("category").Current.Select("/tags/tag") )
{
/// Your Code
}

Resources