Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Tom Cloud
  Where is Tom Cloud?
 Austin
 Texas - United States
 Tom Cloud
 Tags
Subject: get MSXML Node level / depth
Thread ID: 288864 Message ID: 288864 # Views: 43 # Ratings: 0
Version: Visual FoxPro 9 SP2 Category: XML
Date: Friday, December 24, 2010 8:07:30 PM         
   


I'm using MSXML DOM to retrieve data from some large and "deep" source files.

Is there some way I can know how many levels deep below the root a NodeName is?

Below is the recursive procedure I'm using. The lnIndent variable gives an idea of the level, but the many irrelevant (ignored) elements cause it to not be useful.

PROCEDURE DisplayNode(childNodes AS oXML.childNodes, ;
		lnIndent AS INTEGER )
	*
	gnCurIndent = lnIndent
	gcIndent = TRANSFORM(lnIndent) + '-'
	lnIndent = lnIndent + 1
	gcIndent = gcIndent + TRANSFORM(lnIndent)
	Indent = REPLICATE(TAB, lnIndent)
	*
	FOR EACH xNode IN childNodes
		*
		*IF xNode.NodeName = '#text'
		*IF xNode.nodeType = 1 && (NODE_ELEMENT = 1)
		*IF xNode.nodeType = 3 && (NODE_TEXT = 3)
		IF .T.
			*
			* check for omitted terms
			lbOK = ASCAN(aNames, xNode.parentNode.NodeName, -1, -1, -1, 7) = 0 ;
				AND ASCAN(aValues, xNode.parentNode.NodeValue, -1, -1, -1, 7) = 0
			*
			* check for allowed terms
			lbOK = ASCAN(aUseNames, xNode.parentNode.NodeName, -1, -1, -1, 7) > 0
			*
			lbOK = lbOK AND ATC('#TEXT', UPPER(ALLTRIM(xNode.NodeName))) = 1
			*lbOK = .T.
			*
			IF lbOK
				*
				cTmp = UPPER(ALLTRIM(xNode.parentNode.NodeName))
				DO CASE
					CASE cTmp == 'DISPLAY_TEXT'
						gcDisplay = ALLTRIM(xNode.NodeValue)
						* print last submenu item
						IF gbNewMenu
							gcHTML = gcHTML + PrintRow(gcIndent)
						ENDIF
						* print blank row
						gcHTML = gcHTML + ' ' + CRLF ;
							+ '   ' + CRLF + ' ' + CRLF
						gbNewMenu = .F.
						gcNodes = gcNodes + CRLF + xNode.NodeValue + CRLF

					CASE cTmp == 'CAPTION'
						*
						* make new html row
						IF gbNewMenu
							* print previous one, if exists
							gcHTML = gcHTML + PrintRow(gcIndent)
						ELSE
							* print main menu row
							gcHTML = gcHTML + ' ' + CRLF ;
								+ '  ' + TRANSFORM(gcIndent) + '' + CRLF ;
								+ '  ' ;
								+ gcDisplay + '' + CRLF ;
								+ '  ' ;
								+ gcToolTip + '' + CRLF + ' ' + CRLF
						ENDIF
						gbNewMenu = .T.
						gcCaption = ALLTRIM(xNode.NodeValue)
						gcButton = ''
						gcDisplay = ''
						gcHint = ''
						gcToolTip = ''
						gcLink = ''
						gcPath = ''
						gcURL = ''
						*
						gcNodes = gcNodes + CRLF

					CASE cTmp == 'DEFAULT_BUTTON_TEXT'
						gcButton = ALLTRIM(xNode.NodeValue)

					CASE cTmp == 'HINT'
						gcHint = ALLTRIM(xNode.NodeValue)

					CASE cTmp == 'BUTTON_TOOLTIP'
						gcToolTip = ALLTRIM(xNode.NodeValue)

					CASE cTmp == 'LINK'
						gcLink = ALLTRIM(xNode.NodeValue)

					CASE cTmp == 'PATH'
						gcPath = ALLTRIM(xNode.NodeValue)

					CASE cTmp == 'URL'
						gcURL = ALLTRIM(xNode.NodeValue)
				ENDCASE
				*
				gcNodes = gcNodes + TRANSFORM(xNode.nodeType) + ;
					+ '-' + gcIndent + ;
					+ SUBSTR(Indent, nIndentOffset) ;
					+ xNode.parentNode.NodeName
				*
				gcNodes = gcNodes + ':'
				*
				IF !ISNULL(ALLTRIM(xNode.NodeValue))
					gcNodes = gcNodes + ALLTRIM(xNode.NodeValue)
					*gcNodes = gcNodes + ALLTRIM(xNode.NodeName) ;
					+ ':' + ALLTRIM(xNode.NodeValue)
				ELSE
					gcNodes = gcNodes + '<null>'
				ENDIF
				gcNodes = gcNodes + CRLF
				*
				*? SPACE(lnIndent) + xNode.parentNode.nodeName ;
				+ ":" + xNode.nodeValue
				*
				*gcNodes = gcNodes + Indent + xNode.parentNode.nodeName ;
				+ ":" + ALLTRIM(xNode.nodeValue) + CRLF
			ENDIF
		ENDIF

		IF xNode.hasChildNodes
			DisplayNode(xNode.childNodes, lnIndent)
		ENDIF
	NEXT xNode
	*
	RETURN gcNodes
ENDPROC


ENTIRE THREAD

get MSXML Node level / depth Posted by Tom Cloud @ 12/24/2010 8:07:30 PM
RE: get MSXML Node level / depth Posted by Ony Too @ 12/26/2010 8:29:02 AM