SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 6 of 6
  1. #1
    SQA Knight bklabel1's Avatar
    Join Date
    Sep 2012
    Location
    Kew Gardens, United States
    Posts
    2,596
    Post Thanks / Like
    Blog Entries
    1
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)
    Total Downloaded
    0

    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.

    Thanks,

    Kevin

  2. #2
    SQA Knight bklabel1's Avatar
    Join Date
    Sep 2012
    Location
    Kew Gardens, United States
    Posts
    2,596
    Post Thanks / Like
    Blog Entries
    1
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)
    Total Downloaded
    0
    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
    Exit For
    Else
    RetBranchExists = True
    End If
    ' 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
    ddA.RemoveAll
    For Counter = 1 to colAttributes.Count
    Set oAttribute = colAttributes.Item(Counter)
    'print "Element " & firstElement.ElementName & ": " & oAttribute.Name & " = " & oAttribute.Value
    ddA.Add oAttribute.Name,oAttribute.Value
    Next
    Next ' Leave Early
    ' oXML = Nothing
    'oRoot = Nothing
    'colElements = Nothing
    'colAttributes = Nothing
    'oAttribute = Nothing
    ' Set final Status
    RetStatus = True
    End Function

    Thanks,

    Kevin

  3. #3
    Advanced Member
    Join Date
    May 2007
    Posts
    609
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    2 Thread(s)
    Total Downloaded
    0
    If you want to grab an element with an attribute set to a particular value, you can specify that in your xpath search.
    Code:
    set colElements = oRoot.ChildElementsByPath("ElementName[@AttributeName='AttributeValue']")
    Dennis Belew

  4. #4
    Moderator
    Join Date
    Jul 2005
    Location
    Delhi
    Posts
    15,575
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    7 Thread(s)
    Total Downloaded
    0
    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

  5. #5
    Advanced Member
    Join Date
    May 2007
    Posts
    609
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    2 Thread(s)
    Total Downloaded
    0
    Quote Originally Posted by tarunlalwani View Post
    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.

    Tarun, you should be wrong on occasion, just to make the rest of us feel like we're doing something.
    Dennis Belew

  6. #6
    SQA Knight bklabel1's Avatar
    Join Date
    Sep 2012
    Location
    Kew Gardens, United States
    Posts
    2,596
    Post Thanks / Like
    Blog Entries
    1
    Mentioned
    2 Post(s)
    Tagged
    2 Thread(s)
    Total Downloaded
    0
    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.
    Thanks,

    Kevin

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 12.50%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 08:30 AM.

Copyright BetaSoft Inc.