Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Parsing XML With The XMLDocument Object
Parse XML documents in ASP.NET with the XMLDocument object.

| Hungry Eyes |

ASP.NET uses the Document Object Model (DOM) to parse an XML document. DOM builds a tree representation of the XML data structures in the document, and then offers built-in methods to navigate through this tree. Once a particular node has been reached, built-in properties can be used to obtain the value of the node, and use it within the script.

To illustrate this, I'll begin with something simple. Consider the following XML file, a XML-encoded menu for the friendly neighbourhood fast-food joint.


<menu>
<item id="1">
      <name>Hamburger</name>
      <price currency="USD">1</price>
      <size units="oz">3.70</size>
</item>
<item id="2">
      <name>French Fries</name>
      <price currency="USD">1.2</price>
      <size units="oz">3.20</size>
</item>
<item id="3">
      <name>Apple Pie</name>
      <price currency="USD">1.5</price>
      <size units="oz">7.80</size>
</item>
</menu>


Now, I need to parse this so that I can use the data within the elements. Here's a simple ASP.NET script that initializes the parser and reads the XML file.


<%@ Page Language="C#"%>
<%@ import  namespace="System.Xml"%>
<html>
<head>
<script runat="server">
void NodeDetails(XmlNode objNode) {

   // print details of the root element
   output.Text = "Node Name: " + objNode.Name + "<br />";
   output.Text += "Node Type: " + objNode.NodeType + "<br />";
  
   // check if the Element has child nodes
   if(objNode.HasChildNodes) {
      output.Text += "Node Name: " + objNode.FirstChild.Name + "<br />";
      output.Text += "Node Type: " + objNode.FirstChild.NodeType + "<br />";
   }

}

void Page_Load() {

   // location of XML file
   string strXmlDoc = "http://localhost:2121/xml/menu.xml";;
  
   // create an instance of XmlDocument object
   XmlDocument objXmlDoc = new XmlDocument();
  
   // load the XML file into the XmlDocument object
   objXmlDoc.Load(strXmlDoc);
  
   // access the root element of the XML file
   XmlElement objRootElem = objXmlDoc.DocumentElement;
  
   // get details about the root node and its children
   NodeDetails(objRootElem);
  
}
</script>
</head>
<body>
<asp:label id="output" runat="server" />
</body>
</html>


Load this example in your browser to get the following output.

Output image

Now, this might not look like much, but it demonstrates the basic concept of the DOM, and builds the foundation for more complex code. Let's look at the code in detail:

1. The first step is to import all the classes required to execute the application. First come the .NET libraries for the XML parser.


<%@ import  namespace="System.Xml"%>


2. Within the Page_Load() function, I start by defining some variables and objects. The first is a string variable to store the location of the XML file, and the second is a local instance of the XMLDocument object.


<%
   // location of XML file
   string strXmlDoc = "http://localhost:2121/xml/menu.xml";;
  
   // create an instance of XmlDocument object
   XmlDocument objXmlDoc = new XmlDocument();
%>


3. The next step is to load the XML file in memory - this is possible using the Load() method of the XmlDocument object. The end result of this process is a DOM tree consisting of a single root and its child nodes, each of which exposes methods that describe the object in greater detail.


<%
   // load the XML file into the XmlDocument object
   objXmlDoc.Load(strXmlDoc);
%>


4. To get to the root of the DOM tree, I've used the "DocumentElement" property of the XmlDocument object. - this useful property always returns the root element of an XML file.


<%
   // access the root element of the XML file
   XmlElement objRootElem = objXmlDoc.DocumentElement;
%>


5. Once a reference to a node has been obtained, a number of properties become available to obtain the name and value of that node, as well as references to parent and child nodes. In the code snippet below, I've used the "NodeType" and "Name" properties of the XmlNode object to obtain the type and name of the node respectively. Similarly, the HasChildNodes() method can be used to find out if a node has child nodes under it, while the "FirstChild" property can be used to get a reference to the first child node.


<%
void NodeDetails(XmlNode objNode) {

   // print details of the root element
   output.Text = "Node Name: " + objNode.Name + "<br />";
   output.Text += "Node Type: " + objNode.NodeType + "<br />";
  
   // check if the Element has child nodes
   if(objNode.HasChildNodes) {
      output.Text += "Node Name: " + objNode.FirstChild.Name + "<br />";
      output.Text += "Node Type: " + objNode.FirstChild.NodeType + "<br />";
   }

}

%>


If you're sharp-eyed, you'll notice that the "DocumentElement" property returns an object of class XmlElement, whereas my NodeDetails() function above accepts a XmlNode object as a input parameter. The reason is simple: the XmlElement object inherits all the methods and properties of the XmlNode object, as is clear from the documentation available at the following URL: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemxmlxmlelementclasstopic.asp?frame=true. Hence, all XmlElement objects can be cast (implicitly) as XmlNode objects  (though the reverse is not true).


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback