dwadek
asked on
Counting Nodes in a DOM Document Object
I am new to XML and am trying to find some information for navigating an XML document using ASP with the MSXML object (MSXML2.DOMDocument.3.0).
I am trying to count the number of child nodes with a certain name and then load an array with all the elements in that node.
Example:
<DOCUMENT>
<MY_INFO>
<QMTB_WERKS>P000</QMTB_WER KS>
<VERWMERKM>50000524</VERWM ERKM>
<KURZTEXT>FLASH POINT, C, PMCC</KURZTEXT>
<CSOLLWERT>85</CSOLLWERT>
<SOLLWNI>X</SOLLWNI>
<CTOLERANZB />
<TOLOBNI />
<CTOLERANZN />
<TOLUNNI />
<ZPROPERTY>Physical</ZPROP ERTY>
</MY_INFO>
<MY_INFO>
<QMTB_WERKS>P000</QMTB_WER KS>
<VERWMERKM>50000569</VERWM ERKM>
<KURZTEXT>LBS PER GAL @ 15.6 C</KURZTEXT>
<CSOLLWERT>7.69</CSOLLWERT >
<SOLLWNI>X</SOLLWNI>
<CTOLERANZB />
<TOLOBNI />
<CTOLERANZN />
<TOLUNNI />
<ZPROPERTY>Physical</ZPROP ERTY>
</MY_INFO>
</DOCUMENT>
Using this example, I would like to count the number of nodes named "MY_INFO". Then I would like to loop through only those nodes and load each element of those nodes into an array for display within an HTML page at a later time.
Thanks for your help!
DW
I am trying to count the number of child nodes with a certain name and then load an array with all the elements in that node.
Example:
<DOCUMENT>
<MY_INFO>
<QMTB_WERKS>P000</QMTB_WER
<VERWMERKM>50000524</VERWM
<KURZTEXT>FLASH POINT, C, PMCC</KURZTEXT>
<CSOLLWERT>85</CSOLLWERT>
<SOLLWNI>X</SOLLWNI>
<CTOLERANZB />
<TOLOBNI />
<CTOLERANZN />
<TOLUNNI />
<ZPROPERTY>Physical</ZPROP
</MY_INFO>
<MY_INFO>
<QMTB_WERKS>P000</QMTB_WER
<VERWMERKM>50000569</VERWM
<KURZTEXT>LBS PER GAL @ 15.6 C</KURZTEXT>
<CSOLLWERT>7.69</CSOLLWERT
<SOLLWNI>X</SOLLWNI>
<CTOLERANZB />
<TOLOBNI />
<CTOLERANZN />
<TOLUNNI />
<ZPROPERTY>Physical</ZPROP
</MY_INFO>
</DOCUMENT>
Using this example, I would like to count the number of nodes named "MY_INFO". Then I would like to loop through only those nodes and load each element of those nodes into an array for display within an HTML page at a later time.
Thanks for your help!
DW
ASKER
Is the example you give written in Perl or JavaScript?
I've been trying to use selectNodes within
my ASP page using VBScript to no avail.
I have a loaded MSXML DOMDocument Object already but haven't been able to navigate the structure:
<%@ Language=VBScript %>
<% Dim objXML
Set objXML = Server.CreateObject("MSXML 2.DOMDocum ent.3.0")
objXML.loadXML(strOutputXM L)
%>
I'm loading my objXML object with output XML retrieved from a Web Service. I've checked and it is loaded properly and valid (I can display the objXML.xml).
I've been trying to use selectNodes within
my ASP page using VBScript to no avail.
I have a loaded MSXML DOMDocument Object already but haven't been able to navigate the structure:
<%@ Language=VBScript %>
<% Dim objXML
Set objXML = Server.CreateObject("MSXML
objXML.loadXML(strOutputXM
%>
I'm loading my objXML object with output XML retrieved from a Web Service. I've checked and it is loaded properly and valid (I can display the objXML.xml).
My example is Javascript. I am not very proficient at VBScript, but here is probably what you need:
Set objXML = Server.CreateObject("MSXML 2.DOMDocum ent.3.0")
objXML.loadXML(strOutputXM L)
Set nodelist = doc.selectNodes("//MY_INFO ");
For Each node in nodelist
{
...
}
Set objXML = Server.CreateObject("MSXML
objXML.loadXML(strOutputXM
Set nodelist = doc.selectNodes("//MY_INFO
For Each node in nodelist
{
...
}
ASKER
I think I've translated your example correctly into VBScript ...
-------------------------- ---------- ---------- --------
x = 0
Set nodelist = objXML.selectNodes("//MY_I NFO")
For Each node in nodelist
x = x + 1
Next
Response.Write x
-------------------------- ---------- ---------- ---------
I get 0 for x though. This is what happened to me before when I tried using the selectNodes method. It made me suspicious of the ("//MY_INFO"). Is there anyway to list out the nodes from the root (DOCUMENT) with code like this ???
--------------------------
x = 0
Set nodelist = objXML.selectNodes("//MY_I
For Each node in nodelist
x = x + 1
Next
Response.Write x
--------------------------
I get 0 for x though. This is what happened to me before when I tried using the selectNodes method. It made me suspicious of the ("//MY_INFO"). Is there anyway to list out the nodes from the root (DOCUMENT) with code like this ???
ASKER
The string that I'm using and loading into the objXML object might not be structured like I'm expecting it to be and I would like to verify this structure by looping through the objXML and pulling up the node names and hopefully finding one that says "MY_INFO" at some level.
The XPath "//MY_INFO" should find that node at any level. Two things to try:
(1)Make sure your document loaded properly
if (!objXML.loadXML(strOutput XML))
{
Response.Write "Error"
}
(2)If loaded properly, see what you have loaded
objXML.save("c:\temp\myxml .xml");
or
(if you have a server-side debugger)
Dim xml = objXML.xml
(1)Make sure your document loaded properly
if (!objXML.loadXML(strOutput
{
Response.Write "Error"
}
(2)If loaded properly, see what you have loaded
objXML.save("c:\temp\myxml
or
(if you have a server-side debugger)
Dim xml = objXML.xml
ASKER
I just figured out that the xml being loading into the object had a problem. So now I'm getting closer.
Now my code returns the correct number of nodes with the name "MY_INFO" ... Now for the second part of the question:
"...and then load an array with all the
elements in that node."
Now my code returns the correct number of nodes with the name "MY_INFO" ... Now for the second part of the question:
"...and then load an array with all the
elements in that node."
Is this what you need?
Set children = node.childNodes
Set children = node.childNodes
ASKER
In what context? (i.e. I am able to detect the 7 instances of the node I was looking for (MY_INFO) but now I want to be able to reference all the child node names as well as their values. I'm not sure how to use the code that you suggested within my collection of nodes with
the "MY_INFO")
the "MY_INFO")
ASKER
Using this logic to iterate through the collection:
Set nodelist = objXML.selectNodes("//MY_I NFO")
For Each node in nodelist
IF node.hasChildNodes THEN
{ this is where I need to store get the child node names and values }
End IF
Next
Set nodelist = objXML.selectNodes("//MY_I
For Each node in nodelist
IF node.hasChildNodes THEN
{ this is where I need to store get the child node names and values }
End IF
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all your help. I had to modify the code a little and I'm not sure if the syntax is right with the arrays as I was getting errors, but I'm on the right track from here. Here is the code with the modifications:
Dim i
Set nodelist = objXML.selectNodes("//MY_I NFO")
For Each node in nodelist
IF node.hasChildNodes THEN
Response.Write "<br>"
i = 0
For Each child in node.childNodes
'display child node names & values
Response.Write child.nodeName & " - " & child.text & "<br>"
i= i+1
Next
End IF
Next
Dim i
Set nodelist = objXML.selectNodes("//MY_I
For Each node in nodelist
IF node.hasChildNodes THEN
Response.Write "<br>"
i = 0
For Each child in node.childNodes
'display child node names & values
Response.Write child.nodeName & " - " & child.text & "<br>"
i= i+1
Next
End IF
Next
Example:
var doc = new ActiveXObject("Msxml2.DOMD
doc.load("yourfile.xml");
var nodelist = doc.selectNodes("//MY_INFO
var len = nodelist.length;
for (var i=0; i<len; i++)
{
var node = nodelist.item(i);
...
}