QTP-21 Working with XML 与XML交互
来源:互联网 发布:sqlserver创建用户语句 编辑:程序博客网 时间:2024/04/27 15:33
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>
- QTP-21 Working with XML 与XML交互
- QTP - 23 (Working with MS Excel) QTP与Excel交互
- QTP -20 Working with web Tables 与webTable交互
- QTP-22 Working with Databases 与DB交互
- QTP - 24 Working with MS Outlook 与outlook交互
- QTP - 27 Working with HP’s QC 与QC交互
- QTP -25 Working with MS Word 与word交互
- QTP - 17 Working with APIs 与windows API交互
- QTP - 26 Working with MS IE browser 与IE 交互
- Working with XML nodes
- Working with XML nodes
- Working with XML on Android
- javascript与xml交互
- java 与 XML 交互
- xml与数据库交互
- SQL与XML交互
- ajax与XML交互
- Manual:Core:Working with JSON (EXT 与json的交互)
- 网络求职的八大歪招
- C语言使用注意事项(二)
- DATASTAGE:客服端dscc命令使用
- 用“域”来往Word中插入图片
- C语言使用注意事项(三)
- QTP-21 Working with XML 与XML交互
- Android startActivityForResult的使用
- windows服务注册
- JavaScript 仿LightBox内容显示效果
- 自信心在一次次的否定中崩溃了
- 查看本机开放的端口号,查看某个端口号是否被占用,查看被占用的端口号被哪个进程所占用,如何结束该进程
- 提高面试代码质量的三要素
- occ sample geometry 分析初探1
- HDOJ 1021:Fibonacci Again 找出循环周期