#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    65
    Rep Power
    15

    In XML file, how to find out the node(s) that have specific attribute?


    I have the root node object obtained (was easy!) but unable to figure out how to obtain all the nodes that have a specific attribute...for example say the attribute is blue, then how can I obtain a node list of all nodes that have blue as part of their attribute...any ideas are appreciated.

    In case you want to see what I have done so far - here it is. Obviously, the code below doesn't work.

    Code:
    Set xmldoc = CreateObject("Microsoft.XMLDOM")
    sDbcPath = "D:\database\QReleaseLib.dbc"
    xmldoc.Load(sDbcPath)
    
    Set oColNodes = xmldoc.selectNodes("//blue")
    
    For i = 0 To oColNodes.length
    	sMsg = sMsg & i & vbCrLf
    Next
    
    MsgBox sMsg
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
    The SelectNodes query would be like
    Code:
    //node[@attribute='blue']
    (of course "node" and "attribute" have to be changed to their respective values)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    65
    Rep Power
    15
    Originally Posted by requinix
    The SelectNodes query would be like
    Code:
    //node[@attribute='blue']
    (of course "node" and "attribute" have to be changed to their respective values)
    This is not making sense to me - are you sure about this? For one did you mean double quotes instead of single quotes Or aren't you supposed to have double quotes somewhere in there?
  6. #4
  7. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
    Originally Posted by Ranakhu
    This is not making sense to me - are you sure about this?
    Fairly.
    Originally Posted by Ranakhu
    For one did you mean double quotes instead of single quotes
    No. But you can use double quotes if you want.
    Originally Posted by Ranakhu
    Or aren't you supposed to have double quotes somewhere in there?
    Supposed to? Why?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    65
    Rep Power
    15
    So the following line does not work for me i.e. get syntax error

    Code:
    Set oCollNodes = xmldoc.selectNodes(//xmlnode[@LibraryName='ATE'])
    Following line does work for me i.e. no syntax error but I get no results.

    Code:
    Set oCollNodes = xmldoc.selectNodes("//xmlnode[@LibraryName='ATE']")
    Below is my sample XML file - and I want to grab the nodes that have attribute LibraryName no matter what the value is.

    <?xml version="1.0" encoding="UTF-8"?>
    <dbc version="1.0">
    <CObject0 overallSchema="21" flag="DX Databook Overlay Configuration File" BaseConfigurationURL="">
    <CConfig1>
    <CConfigSym2>
    <CObArray3 CXMLTypedPtrArraySize="0"/>
    </CConfigSym2>
    <CConfigLib4>
    <CObArray5 CXMLTypedPtrArraySize="29">
    <CConfigLibEntry6 LibraryName="ATE" JoinTable="false" Locked="0" SymbolExpression="" JoinType="0" HorizJoinType="0">
    <CConfigAtt7 UseCentralLibrarySymbols="1">
    <CObArray8 CXMLTypedPtrArraySize="38">
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    65
    Rep Power
    15
    I got it - the following works:

    Code:
    Set oCollNodes = xmldoc.selectNodes("//@LibraryName")
    So, double quotes is required (otherwise I get syntax error!). And, by excluding the node (name) in parenthesis, I can select all nodes which have LibraryName as attribute.
  12. #7
  13. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
    Yes, all I gave you was the query. It still has to be a string when you pass it. And "xmlnode" is not the name of the node either.

    What you're doing is selecting the LibraryName attributes.
    Code:
    //*[@LibraryName]
    That is what you say you want: all nodes which have LibraryName as an attribute. And that also has to be a string.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2009
    Posts
    65
    Rep Power
    15
    Originally Posted by requinix
    Yes, all I gave you was the query. It still has to be a string when you pass it. And "xmlnode" is not the name of the node either.

    What you're doing is selecting the LibraryName attributes.
    Code:
    //*[@LibraryName]
    That is what you say you want: all nodes which have LibraryName as an attribute. And that also has to be a string.
    Did I imply that "xmlnode" is the name of the node? Not following what you mean...
  16. #9
  17. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,754
    Rep Power
    9397
    You said your code was
    Code:
    Set oCollNodes = xmldoc.selectNodes("//xmlnode[@LibraryName='ATE']")
    That there says "xmlnode". You said your XML was
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <dbc version="1.0">
    <CObject0 overallSchema="21" flag="DX Databook Overlay Configuration File" BaseConfigurationURL="">
    <CConfig1>
    <CConfigSym2>
    <CObArray3 CXMLTypedPtrArraySize="0"/>
    </CConfigSym2>
    <CConfigLib4>
    <CObArray5 CXMLTypedPtrArraySize="29">
    <CConfigLibEntry6 LibraryName="ATE" JoinTable="false" Locked="0" SymbolExpression="" JoinType="0" HorizJoinType="0">
    <CConfigAtt7 UseCentralLibrarySymbols="1">
    <CObArray8 CXMLTypedPtrArraySize="38">...
    There is no "xmlnode" in there.

    That's what I was pointing out. That's the reason it didn't work.

IMN logo majestic logo threadwatch logo seochat tools logo