Tree-Walking Example

The following code traverses a DOM tree using pre-order traversal.

PRO sample_recurse, oNode, indent 
 
   ; "Visit" the node by printing its name and value 
   PRINT, indent GT 0 ? STRJOIN(REPLICATE(' ', indent)) : '', $ 
      oNode->GetNodeName(), ':', oNode->GetNodeValue() 
 
   ; Visit children 
   oSibling = oNode->GetFirstChild() 
   WHILE OBJ_VALID(oSibling) DO BEGIN 
      SAMPLE_RECURSE, oSibling, indent+3 
      oSibling = oSibling->GetNextSibling() 
   ENDWHILE 
END 
 
PRO sample 
   oDoc = OBJ_NEW('IDLffXMLDOMDocument') 
   oDoc->Load, FILENAME="sample.xml" 
   SAMPLE_RECURSE, oDoc, 0 
   OBJ_DESTROY, oDoc 
END 

This program generates the following output for the plug-in file (see About the DOM Structure):

#document: 
   plugin: 
      #text: 
 
      name: 
         #text:Weather.com Radar Image [DEN] 
      #text: 
 
      description: 
         #text:600 mile Doppler radar image for DEN 
      #text: 
 
      version: 
         #text:1.0 
      #text: 
 
      tab: 
         #text: 
 
         icon: 
            #text:weather.gif 
         #text: 
 
         tooltip: 
            #text:DEN Doppler radar image 
         #text: 
 
      #text: 

The program above created an IDLffXMLDOM object for every node it encountered and did not destroy them until the document was destroyed. Another approach, illustrated in the program below, cleans up the nodes as it proceeds:

PRO sample_recurse2, oNode, indent 
   ;; "Visit" the node by printing its name and value 
   PRINT, indent gt 0 ? STRJOIN(REPLICATE(' ', indent)) : '', $  
      oNode->GetNodeName(), ':', oNode->GetNodeValue() 
 
   ;; Visit children 
   oNodeList = oNode->GetChildNodes() 
   n = oNodeList->GetLength() 
   for i=0, n-1 do $ 
      SAMPLE_RECURSE2, oNodeList->Item(i), indent+3 
   OBJ_DESTROY, oNodeList 
END 
 
PRO sample2 
   oDoc = OBJ_NEW('IDLffXMLDOMDocument') 
   oDoc->Load, FILENAME="sample.xml" 
   SAMPLE_RECURSE2, oDoc, 0 
   OBJ_DESTROY, oDoc 
END 

Please note that document and text nodes do not have node names, so the GetNodeName method always returns `#document' and `#text,' respectively.