Results 1 to 5 of 5

Thread: How to reference duplicate Node Names

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    37

    How to reference duplicate Node Names

    I have an XML file that literally has hundreds of child nodes that have the same name.
    I am need to update a few of them with values.
    The child nodes look like this:
    Code:
    <GA>
    	<GASM>
    		<GASMT>Single Family</GASMT>
    		<GSSMEx>0</GSSMEx>
    		<GASMf>0</GASMf>
    	</GASM>
    	<GASM>
    		<GASMT>1956</GASMT>
    		<GSSMEx>0</GSSMEx>
    		<GASMf>0</GASMf>
    	</GASM>
    	<GASM>
    		<GASMT>Standard Home Inspection</GASMT>
    		<GSSMEx>0</GSSMEx>
    		<GASMf>0</GASMf>
    	</GASM>
    	<GASM>
    		<GASMT>Occupied</GASMT>
    		<GSSMEx>0</GSSMEx>
    		<GASMf>0</GASMf>
    	</GASM>
    	<GASM>
    		<GASMT>Overcast</GASMT>
    		<GSSMEx>0</GSSMEx>
    		<GASMf>0</GASMf>
    	</GASM>
    	<GASM>
    		<GASMT>Wood-Destroying Insect</GASMT>
    		<GSSMEx>0</GSSMEx>
    		<GASMf>0</GASMf>
    	</GASM>
    </GA>
    I figured out that I need to get a node list. There are 8 in the node list. I am using:
    Set objNodeList = DOM.documentElement.SelectNodes("//Report/templatehma/genInfo/GA/GASM/GASMT")

    This produces the typed value for each node"
    MsgBox objNodeList.Item(0 through 7).nodeTypedValue

    However, when I try to create a single node to update the nodes, the first node is updated, EG
    Set Node = DOM.SelectSingleNode("//" & objNodeList.Item(1 through 7).nodeName) references the first node, which is item(0)

    How do I reference each node other than the first node?

    Thanks for your help
    Last edited by mhnvb; Nov 6th, 2014 at 01:52 AM. Reason: Additonal information

  2. #2
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    759

    Re: How to reference duplicate Node Names

    What's wrong with

    Code:
    Set objNodeList = DOM.documentElement.SelectNodes( _
       "//Report/templatehma/genInfo/GA/GASM/GASMT" _
       )
    
    Set first = objNodeList( 0 )
    Or, using XPath:

    Code:
    Set objSingle = DOM.documentElement.SelectSingleNode( _
       " . / descendant-or-self:: Report / templatehma / genInfo / GA / GASM / GASMT [ 1 ] " _
       )
    Please note my expansion of the "//" token which does not mean the root of the document!

    Using the "descendant-or-self" axis tells XPath to find [a set of] Nodes with the given Node Name anywhere in the document, regardless of the intervening structure. You could just as easily (and, IMHO, wrongly, say):

    Code:
    Set objSingle = DOM.documentElement.SelectNodes( _
       " . / descendant-or-self:: GASMT " _
       )
    Regards, Phill W.

  3. #3
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: How to reference duplicate Node Names

    How do you know which GASMT nodes need to be updated?

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    37

    Re: How to reference duplicate Node Names

    Phil,
    Thanks very much.
    Your first example was what I was able to make work.
    The "Meat" of the fix is the line : Set Node = objNodeList(N) where N is the item I need to reference.
    The line I was using was from a different project of mine where I needed to set attributes.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2012
    Posts
    37

    Re: How to reference duplicate Node Names

    Mark,
    Right now, I am just going by the order in which they appear. I dont see any specific code that indicates which node is which.
    Phil's answer works for me.
    Thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width