Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
DOM to vfp class
Message
De
12/02/2009 11:04:21
 
 
Information générale
Forum:
Visual FoxPro
Catégorie:
XML, XSD
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web Service
Divers
Thread ID:
01381075
Message ID:
01381284
Vues:
63
>Well, xmladapter is not working for us in this particular case. The schema is supposedly very complex, and xmladapter complains of something like 'duplicate node.'
>
>I can 'load' the xml in question using xmldom just fine. But to write a generic recursive routine that 'fills' the properties of a custom business class can be a complex/time-consuming task.
>
>I was hoping someone here would know of a vfp utility/tool that already does that.
>
>--Sid.
>
Sid,

I'm working on something like that - but it is far from finished

To traverse is not that difficult

I'm working on a class
I define two properties
- a (callback) function that is called before the node is traversed
- a (callback) function that is called after the node is traversed

It works with callbacks - so the traverse routine is reusable

See sample. DumpToFile()

Hope it helps a bit - not fully tested yet
function DumpToFile(FileName)

	local Success
	Success = TRUE
	
	local fd
	
	do case
	case !m.Success
	
	case !isObject(m.this.DomDocument_Obj)
		assert FALSE
		Success = FALSE
	
	case !IsChar(m.FileName)
		assert FALSE
		Success = FALSE
		
	otherwise
		fd = fcreate(m.FileName)
		
	endcase
	
	do case
	case !m.Success
	
	case m.fd < 0 
		assert FALSE
		Success = FALSE
	

	case !m.this.AddProperty('DumpToFile_fd', m.fd)
		assert FALSE
		Success = FALSE
	
	case !m.this.Traverse('m.this.DumpToFile_Pre', 'm.this.DumpToFile_Post')
		assert FALSE
		Success = FALSE
		
	endcase
	
	=fclose(m.fd)
	
	return m.Success
	
endfunc
*-------------------------------------------------------------------------------
protected function DumpToFile_Pre(Node, lvl)

	local Success
	Success = TRUE
	
	local s
	
	do case
	case !m.Success
	
	case inlist(m.Node.NodeType, XMLNODETYPE_NODE_TEXT)
		&& ignore
		
	case !m.this.NodeTypeString(m.Node.NodeType, @m.s)
		assert FALSE
		Success = FALSE
	
	otherwise
		= fputs(	m.this.DumpToFile_fd, ;
					repl('.', m.lvl) ;
						+ '<' ;
						+ m.Node.nodeName ;
						+ ',' ;
						+ transf(m.lvl) ;
						+ ',' ;
						+ m.s ;
						+ '>' ;
						+ transf(m.Node.nodeValue) ;
				)
	endcase
	
	return m.Success
	
endfunc
*-------------------------------------------------------------------------------
protected function DumpToFile_Post(Node, lvl)

	local Success
	Success = TRUE
	
	local s
	
	do case
	case !m.Success
	
	case inlist(m.Node.NodeType, XMLNODETYPE_NODE_TEXT)
		&& ignore
		
	case !m.this.NodeTypeString(m.Node.NodeType, @m.s)
		assert FALSE
		Success = FALSE
	
	otherwise
		= fputs(	m.this.DumpToFile_fd, ;
					repl('.', m.lvl) ;
						+ '</' ;
						+ m.Node.nodeName ;
						+ ',' ;
						+ transf(m.lvl) ;
						+ ',' ;
						+ m.s ;
						+ '>' ;
				)
	endcase
	
	return m.Success
	
endfunc
*-------------------------------------------------------------------------------
function Traverse(TraverseFunction_Pre, TraverseFunction_Post)

	local Success
	Success = TRUE
	
	do case
	case !m.Success
	
	case !isNull(m.TraverseFunction_Pre) and !IsChar(m.TraverseFunction_Pre)
		assert FALSE
		Success = FALSE
	
	case !isNull(m.TraverseFunction_Post) and !IsChar(m.TraverseFunction_Post)
		assert FALSE
		Success = FALSE
	
	case isnull(m.TraverseFunction_Pre) and isnull(m.TraverseFunction_Post)
		assert FALSE
		Success = FALSE
	
	endcase
	
	do case
	case !m.Success
	
	case isnull(m.TraverseFunction_Pre) 
		if( !m.this.AddProperty('TraverseCall_Pre', [TRUE]) )
			assert FALSE
			Success = FALSE
		endif
		
	case !m.this.AddProperty('TraverseCall_Pre', m.TraverseFunction_Pre + '(m.Node, m.lvl)')
		assert FALSE
		Success = FALSE
	
	endcase
	
	do case
	case !m.Success
	
	case isnull(m.TraverseFunction_Post) 
		if( !m.this.AddProperty('TraverseCall_Post', [TRUE]) )
			assert FALSE
			Success = FALSE
		endif

	case !m.this.AddProperty('TraverseCall_Post', m.TraverseFunction_Post + '(m.Node, m.lvl)')
		assert FALSE
		Success = FALSE
	
	endcase
	
	do case
	case !m.Success
	
	case !m.this.Traverse_One(m.this.DomDocument_Obj.DocumentElement, 0)
		assert FALSE
		Success = FALSE
	
	case !m.this.AddProperty('TraverseCall_Pre', null)
		assert FALSE
		Success = FALSE
	
	case !m.this.AddProperty('TraverseCall_Post', null)
		assert FALSE
		Success = FALSE
		
	endcase
	
	return m.Success
	
endfunc
*-------------------------------------------------------------------------------
protected function Traverse_One(Node, lvl)

	local Success
	Success = TRUE
	
	local i, ChildNode
	
	do case
	case !m.Success
	
	case !eval(m.this.TraverseCall_Pre)
		assert FALSE
		Success = FALSE

	case !inlist(m.Node.nodeType, XMLNODETYPE_NODE_ELEMENT)
	
	otherwise
		
		with m.Node.Attributes
			for i = 0 to .Length - 1
				
				if( !m.this.Traverse_One(.Item(m.i), m.lvl+1) )
					assert FALSE
					Success = FALSE
					exit
				endif
			endfor
		endwith
	
	endcase
	
	
	do case
	case !m.Success
	
	otherwise
		ChildNode = m.Node.firstChild
		
		do while m.Success and !IsNull(m.ChildNode)
			
			if( !m.this.Traverse_One(m.ChildNode, m.lvl+1) )
				assert FALSE
				Success = FALSE
				exit
			
			endif
			
			ChildNode = m.ChildNode.nextSibling
			
		
		enddo
	
	endcase
	
	do case
	case !m.Success
	
	case !eval(m.this.TraverseCall_Post)
		assert FALSE
		Success = FALSE
	
	endcase
	
	
	return m.Success
	
endfunc
*-------------------------------------------------------------------------------
Gregory
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform