If you are frequently parsing XML files in your database, you may find it useful to display a progress bar or message to your user to let them know that something is happening. To do this, you must be able to determine a start point and an end point.
Using this technique, you can count the number of XML elements contained in your XML document, which can be used later for things like progress indicators. In this example, the method is named CountAllElements.
` CountAllElements Method
C_TEXT($1;$rootElement)
C_LONGINT($0;$2;$count)
$rootElement:=$1
$count:=$2+1
C_TEXT($child)
$child:=Get First XML element($rootElement)
While (OK=1)
` Here it calls itself. This recursive call allows us to crawl the XML tree.
$count:=CountAllElements ($child;$count)
$child:=Get Next XML element($child)
End while
$0:=$count
Supposing we had a file called "test.xml", we could use the following to count elements:
$count:=0
$root:=Parse XML source("test.xml")
$count:=CountAllElements ($root;$count)
CLOSE XML($root)
The XML document like the following yields a count of 3:
<a>
<b>
<c/>
</b>
</a>
Your only concern is that, since this is a recursive style method, you must make sure to increase your stack size for very large XML files.