Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating through elements 1

Status
Not open for further replies.

durug

Technical User
Mar 22, 2002
335
CA
Hi!

I try to create am xml file in VB, and the problem that I have is when I'm trying to create attributes (in my example the attribute att1) and populate them.

<node1>
<node2 att1=&quot;1&quot;></node2>
<node2 att1=&quot;2&quot;></node2> <-----
</node1>

So here is what happens:
When I create for the second element node2(line 3) the attributes, the program is creating the attributes for the first element node2(line 2) and overwrites the old attributes because I'm using selectsinglenode(&quot;node1/node2&quot;). I know this method is selecting the first node with this name, but how do I get to the second element with the same name?

Thanks a lot,
Durug

 
Apparently nobody moves here :)
So I will make it more complicated.

My xml document looks like this:
<node1>
<node2>
</node2>
<node2>
</node2>
</node1>

How can I insert the node3 to look like this in a loop?
<node1>
<node2>
<node3></node3>
</node2>
<node2>
<node3></node3>
</node2>
</node1>

If I use this lines:
Set newNode = pageXML.createElement(&quot;node3&quot;)
Set contentNode = pageXML.selectSingleNode(&quot;node1/node2&quot;)
contentNode.appendChild (newNode)
He is inserting like this
<node1>
<node2>
<node3></node3>
<node3></node3>
</node2>
<node2>
</node2>
</node1>
And I know why, but I don't know how to fix this!
 
First, I don't know anything about VB, but I've worked extensevly with DOM.
My suggestion would be to get the child nodes for <node1> and check each node - for its name - for inserting where you want the new info to be inserted.

There is a DOM method (specified in the standard):
NodeList Node::getChildNodes()
use that list to navigate through the nodes.

This has to be expressed somehow in VB...
A pseudo:
Node node(&quot;node1&quot;);
NodeList nl = node.getChildNodes();
count = nl.getLength();
for (int i = 0; i < count; i++)
your_checkItem(nl.item(i));

[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
This is how I done it.

Set NodeList = pageXML.selectNodes(&quot;node1/node2&quot;)
For Each Node In NodeList
Set newDetailNode = pageXML.createElement(&quot;node3&quot;)
Node.appendChild (newDetailNode)
Next

Thanks anyway!
Are you from RO?
 
Yep... I am from Romania... Therefore I am [thumbsup].. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Set NodeList = pageXML.selectNodes(&quot;node1&quot;)
For Each Node In NodeList
Set newDetailNode = pageXML.createElement(&quot;node3&quot;)
Node.appendChild (newDetailNode)
Next
codestorm
Fire bad. Tree pretty. - Buffy
Hey, I'm operating on a limited mental budget here.
<insert witticism here>
 
Thanks codestorm, do you have any idea how to add in the same loop attributes for node3?

thanks,
durug
 
Just use newDetailNode.setAttribute(&quot;tag&quot;, &quot;value&quot;) before appending it to the current node.

For Each Node In NodeList
Set newDetailNode = pageXML.createElement(&quot;node3&quot;)
newDetailNode.setAttribute(&quot;tag&quot;, &quot;value&quot;)
Node.appendChild (newDetailNode)
Next

Check for syntax errors, I've been too long away from VB.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top