Introduction to XML Document Object Model - Traversing the Nodes in an XML Document
(Page 5 of 5 )
Parse XML
<FONT face="Verdana, Arial, Helvetica, sans-serif">#document Type:DOCUMENT(9) Value=null
BookAuthors Type:ELEMENT(1) Value:null
Author Type:ELEMENT(1) Value:null
au_id Type:ELEMENT(1) Value:null</FONT><FONT face="Verdana, Arial, Helvetica, sans-serif">
<P class=MsoBodyText2 style="MARGIN: 0in 0in 0pt">
#text Type Text(3) Value:1001
au_lname Type:ELEMENT(1) Value:null
<P class=MsoBodyText2 style="MARGIN: 0in 0in 0pt">
#text Type Text(3) Value:Gates
au_fname Type:ELEMENT(1) Value:null
<P class=MsoBodyText2 style="MARGIN: 0in 0in 0pt">
<P class=MsoBodyText2 style="MARGIN: 0in 0in 0pt">#text Type Text(3) Value: Bill
Author Type:ELEMENT(1) Value:null
au_id Type:ELEMENT(1) Value:null
<P class=MsoBodyText2 style="MARGIN: 0in 0in 0pt">#text Type Text(3) Value:1002
au_lname Type:ELEMENT(1) Value:null
<P class=MsoBodyText2 style="MARGIN: 0in 0in 0pt">#text Type Text(3) Value:Potter
au_fname Type:ELEMENT(1) Value:null
<P class=MsoBodyText2 style="MARGIN: 0in 0in 0pt">#text Type Text(3) Value: Harry
Hope you got a clear and quick picture of the recursive nature of the XML DOM. At the top we have the #document Type node, which is an inherent parent, that means the root node of all XML documents. Pay careful attention though. It’s not actually an element-it has a type of DOCUMENT. So the root of the XML data is an XML document, but under that you have XML Elements.
In our case the first root element is the BookAuthors element. This in turn contains an element for each Author and an element for each property of the Author. We also notice some additional information for each leaf node (i.e. node with no children). We have another node called #text. This actually contains the text of the node. You may ask then why does each element have a value of null and its sub-element called #text contains the value of the node. The answer is very simple. Some nodes may have both, another node, as well as contain text. If a node contains both text and other nodes, what will be the value? Will it be a text or the child node? This led W3C to specify that text for a node be always held in a child node of type Text.
So this leaves us with the final part of the code, for when we accessed the node we did not step deep down to another level in the tree to access the child. We will do it now!
</SPAN><?xml:namespace prefix = o /><o:p> </o:p></FONT>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify"><SPAN style="FONT-SIZE: 11pt; mso-bidi-font-size: 10.0pt"><STRONG><FONT size=2><FONT face="Verdana, Arial, Helvetica, sans-serif"></b> Value: <b>’ + baNode.nodeValue + ‘</b><br>’;<o:p></o:p></FONT></FONT></STRONG></SPAN>
<P class=MsoBodyText2 style="MARGIN: 0in 0in 0pt"><FONT face="Verdana, Arial, Helvetica, sans-serif">
Voila! We have used the nodeValue property, too. This is Microsoft’s simplified extension of DOM. W3C specified that to access the value of the node-you have to traverse to the child to access the associated TEXT node. Microsoft felt that such a common action as accessing the value of a node to get at the text it holds must be simplified, hence intelligently delegated it by introducing nodeValue property to handle the TEXT nodes.
This tutorial has shown you about DOM and how it stores XML data in a tree structure. Now that you understand how XML works and have been introduced to DOM, next we can take a look at how XML integrates with ADO.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |