XMLUTIL same branches and same attributes
My XML has several identical branches. Also the attribute names in each branch are identical. I need to find which branch number that contains an attribute with a specific value.
I will look at XMLUTIL code that I have working. If I make progress, I will put my code here. If anyone can assist me, I'd really apprecitate it.
I can make a sample XML file if it would be useful.
I have a function that finds out how many branches there are. Then it puts all of the attributes and values into a data dictionary. I can probably loop through each branch and collect the attributes. Then check if the value on the branch matches what I am looking for. It seems as if there must be an easier way.
'Provide the path/file, branch path, Branch Number.
' The function returns If the Branch Exists, If any Attributes Exist
' It clears and populates the ddA data dictionary.
Function GetBranchAttributes(mFile,mBranchPath, mBranchNumber,RetBranchExists,ddA,RetStatus)
Dim intNumberOfBranches, LeaveEarly,Counter, oAttribute
Dim oXML, oRoot, colElements,colAttributes, firstElement
RetStatus = False
' Change character to integer.
mBranchNumber = Cint(mBranchNumber)
' This for loop is used to leave the function early and only having to maintain one cleanup.
For LeaveEarly = 1 To 1
' Create the XMLUTIL object
Set oXML = XMLUtil.CreateXMLFromFile(mFile)
' Get the root element
Set oRoot = oXML.GetRootElement
' Put the root name in a variable only for tracing.
mRootName = oRoot.ElementName
' Make a set of Branches.
Set colElements = oRoot.ChildElementsByPath(mBranchPath)
' Determine how many branches there are that match the input path.
intNumberOfBranches = colElements.Count
' Validate that the query branch is with in the bounds of the existing branches.
If mBranchNumber > intNumberOfBranches Then
' Let the outside of the function handle this. In some places is not mandatory for the branch/value to not exist.
'Reporter.ReportEvent micFail,"GetBranchAtrributes() The requested branch Number does not exist.", "Branch requested: " & mBranchNumber & " Number of Branches: " & intNumberOfBranches
RetBranchExists = False
RetBranchExists = True
' Change the number to collect this branches Attributes.
set firstElement = colElements.Item(mBranchNumber) '1 Check the count so we do not go out of bounds.
' Put all of the attributes into an object.
set colAttributes = firstElement.Attributes
' Clear the data dictionary
For Counter = 1 to colAttributes.Count
Set oAttribute = colAttributes.Item(Counter)
'print "Element " & firstElement.ElementName & ": " & oAttribute.Name & " = " & oAttribute.Value
Next ' Leave Early
' oXML = Nothing
'oRoot = Nothing
'colElements = Nothing
'colAttributes = Nothing
'oAttribute = Nothing
' Set final Status
RetStatus = True
If you want to grab an element with an attribute set to a particular value, you can specify that in your xpath search.
set colElements = oRoot.ChildElementsByPath("ElementName[@AttributeName='AttributeValue']")
XMLUtil is just a set of API. Your limitation is the lack of knowledge of XPATH. I would recommend you to read the same from W3Schools.com
Yep, XPATH is extremely handy knowledge if you're going to be working with XML on a regular basis. W3Schools is an excellent resource for learning it.
Originally Posted by tarunlalwani
Tarun, you should be wrong on occasion, just to make the rest of us feel like we're doing something.
Denis and Tarun,
I always appreciate your answers.
I did look at W3Schools a few weeks ago for XPATH. It did get me started.
The part I have trouble with is knowing what things are available in XMLUTIL. The documentation in QTP is scattered from the way I think. I'm looking at the examples in the different methods and properties and either forget they exist or don't clearly see what they do in the examples. I looked at the new QTP book. For me, I could have used more examples. Also the examples did not cover attributes. Maybe the previous book covered it. I know enough now to get started when I run into new XML challenges.