21 Working with XML
Concept: QTP provides a built-in utility object tomanipulate XML, but the function are not documented in QTP user manual butonline Help.
21.1 QTP XML Objects
QTP provides a rich set of utility XML objects:
XMLUtil: The object used to access and return XML objects. |
XMLAttribute: An object representing an XML element attribute. |
XMLAttributesColl: An object representing a collection of element attributes. |
XMLElement: An object representing an XML element. |
XMLElementsColl: An object representing a collection of XML elements. |
XMLData: An object representing an XML block. |
XMLItemColl: An object representing a collection of XML items. |
21.2 How to copy an XML and save it to another XML?
'Method 1 Set XMLObj = XMLUtil.CreateXML() XMLObj.LoadFile("C:\Users\tanxing\Desktop\XMLTest.xml") XMLObj.SaveFile "C:\Users\tanxing\Desktop\XMLTest_2.xml" Set XMLObj = Nothing |
'Method 2 Set XMLObj = XMLUtil.CreateXMLFromFile("C:\Users \XMLTest.xml") XMLObj.SaveFile "C:\Users\tanxing\Desktop\XMLTest_3.xml" Set XMLObj = Nothing |
'Method 3 Set xmlDocument = XMLUtil.CreateXMLfromFile ("C:\Users \XMLTest.xml") sXML = xmlDocument.ToString Set xmlDoc2 = XMLUtil.CreateXML() xmlDoc2.Load sXML xmlDoc2.SaveFile "C:\Users\tanxing\Desktop\XMLTest_2.xml" Set xmlDocument = Nothing Set xmlDoc2 = Nothing |
21.2 How to create the following XML at run-time in QTP, and then save theXML?
XML: <Environment> <Variable> <Name>FirstName</Name> <Value>Tarun</Value> </Variable> <Variable> <Name>LastName</Name> <Value>Lalwani</Value> </Variable> </Environment> |
‘QTP code:
Set xmlDoc=XMLUtil.CreateXML
'Create a document with Environment as the root node xmlDoc.CreateDocument "Environment"
'Get the root nod element. 'The data type returned would be of type XMLElement Set xmlRoot = xmlDoc.GetRootElement()
'Add a new variable node Call xmlRoot.AddChildElementByName("Variable","") Set newNode = xmlRoot.ChildElementsByPath("//Variable")
'Go to the last matching node Set newNode = newNode.Item(newNode.Count)
'Add the name and value child nodes newNode.AddChildElementByName "Name","FirstName" newNode.AddChildElementByName "Value","Tarun"
'Add a new variable node
Call xmlRoot.AddChildElementByName("Variable","") Set newNode = xmlRoot.ChildElementsByPath("//Variable")
'Go to the last matching node Set newNode = newNode.Item(newNode.Count)
'Add the name and value child nodes newNode.AddChildElementByName "Name","LastName" newNode.AddChildElementByName "Value","Lalwani" xmlDoc.SaveFile "C:\Users\tanxing\Desktop\XMLTest_2.xml"
Set newNode = Nothing Set xmlRoot = Nothing Set xmlDoc = Nothing |
21.3 How to load environment variables from multiple XML files?
QTP’s Environment.LoadFromFile has a disadvantage, it doesnot allow load multiple environment files.
Key Code are as following, note that variable in XML shouldnot contain any environment variable from LoadFromFile& Associated environment Libraries(Need function to test this in project).
Set xmlDoc = XMLUtil.CreateXMLFromFile (FileName) Set xmlRoot = xmlDoc.GetRootElement() 'Get all the variables node Set xmlValues = xmlRoot.ChildElementsByPath("Variable") 'Loop through all variable nodes For i= 1 to xmlValues.Count Set xmlElement = xmlValues.Item(i) sName = xmlElement.ChildElementsByPath("Name").item(1).value sValue = xmlElement.ChildElementsByPath("Value").item(1).value Next |
21.4 How can we modify the contents of an XML file?
XML: <Environment> <Variable> <Name>FirstName</Name> <Value>Tarun</Value> </Variable> <Variable> <Name>LastName</Name> <Value>Lalwani</Value> </Variable> </Environment> |
‘QTP code:(修改varNam的值为varVal) Set xmlDoc = XMLUtil.CreateXML () Set xmlRoot = xmlDoc.GetRootElement() Set xmlCol = xmlRoot.ChildElementsByPath ("/Environment/Variable") For i =1 to xmlCol.count
'Look for the Name child from current node If xmlCol.item(i).ChildElementsByPath ("./Name").item(1).Value = varNam Then
'Set the value for Value node xmlCol.item(i).ChildElementsByPath ("./Value").item(1).SetValue varVal Exit Function End if Next |
Note: The ChildElementsByPath allow using relative path. ChildElementsByPathmethod is XPath (more detail: http://www.w3schools.com/xpath/xpath_intro.asp).To validate a unique name in the XML we use \\<NodeName>as the Xpath and validate the value.
21.5 Exporting XML to a DataTable.
思路:把XML的变量用ChildElementsByPath返回一个数组,再加入DataTable中:
For i =1 to xmlCols.count Set xmlCol = xmlCols.item(i) iCount = xmlCol.ChildElements().Count
'Loop through direct childrens For j = 1 to iCount sParamName = xmlCol.ChildElements.item(i).ElementName() sParamValue = xmlCol.ChildElements.item(i).Value() oDataTable.GetParameter(sParamName).Value = sParamValue Next oDataTable.SetCurrentRow i + 1 Next |
21.6 How can we extract XML from browser and save XML?
Note: the same way extract XML from local. Ref the 21.1.
sURL = "www.baidu.com\XMLTest.xml"
SystemUtil.Run "iexplore.exe", sURL Set xmlDoc = Browser("creationtime:=0").WebXML("micClass:=WebXML").GetData() xmlDoc.SaveFile "C:\Users\tanxing\Desktop\XMLTest2.xml" |
21.7 How can we compare two XML documents for equality?
Set XMLObj1 = XMLUtil.CreateXMLFromFile("C:\Users \XMLTest1.xml") Set XMLObj2 = XMLUtil.CreateXMLFromFile("C:\Users \XMLTest2.xml") isEqual = XMLObj1.Compare(XMLObj2) ‘return 1 if equal |
21.8 How can we compare two XML file with different ordering of nodes?
比如下图:不做研究,项目需要时再说。
XML1: <Environment> <Variable> <Name>FirstName</Name> <Value>Tarun</Value> </Variable> </Environment> |
XML2: <Environment> <Variable> <Value>Tarun</Value> <Name>FirstName</Name> </Variable> </Environment> |