-
Nov 5th, 2014, 11:40 PM
#1
Thread Starter
Member
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
-
Nov 6th, 2014, 08:51 AM
#2
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.
-
Nov 6th, 2014, 12:03 PM
#3
Re: How to reference duplicate Node Names
How do you know which GASMT nodes need to be updated?
-
Nov 6th, 2014, 03:42 PM
#4
Thread Starter
Member
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.
-
Nov 6th, 2014, 03:48 PM
#5
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|