A simple recursive descent parsing algorithm for parsing xml which i developed sometime back. Generally a recursive parsing algorithm is an idealistic approach for parsing xml documents with 'tree' sort of a structure. Most of the xml documents can be processed in .net using System.xml ,xpath expressions and various other techniques. But the generality for fetching the data in a certain order becomes important while processing the xml. for brevity's sake i'll consider an xml document which serializes a window.so, ideally the tree structure in the xml would look like the following.
Note the classic 'tree' structure here, i.e a 'container' node can contain other 'container' nodes as child nodes besides 'component' nodes. Therfore i used a recursive algorithm for parsing the tree.
the C# implementation:
internal void RecursiveDescentParseXML(XmlNode root)
{
XmlNode node = root;
Console.WriteLine(root.Name);
if (node.HasChildNodes)
{
XmlNodeList cnodes = node.ChildNodes;
foreach (XmlNode n in cnodes)
{
RecursiveDescentParseXML(n);
}
if (node.Name.ToString().Equals("window")) { }
else
{
Console.WriteLine("Node: " + node.Name +
" Should be added to : " + node.ParentNode.Name);
}
}
else{
Console.WriteLine("Node: " + node.Name +
" Should be added to : " + node.ParentNode.Name);
}
}
No comments:
Post a Comment