ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

This content has been translated automatically.  Click here  to view the French version.
Help / WLanguage / WLanguage functions / Standard functions / XML file functions
  • Example for handling XML files
  • Finding the invoices in an XML file
  • Displaying the content of an XML document
XMLDocument (Example)
Example for handling XML files
WINDEVJavaUser code (UMC)
This example is used to add 2 invoices into an XML file and to save them.
sMyXMLDoc is string = "XML"
sXMLInfo is string // Result string
nIndex is int
nXMLPosition is int

XMLDocument(sMyXMLDoc, "") // Creates a blank XML document

// Invoice 1
// ----------
// Root element
XMLAddChild(sMyXMLDoc, "LIST_OF_INVOICES", "", True)  
// "Parent" node
XMLAddChild(sMyXMLDoc, "INVOICE", "", True)  

// Information about the invoice
XMLAddChild(sMyXMLDoc, "NUMBER", NumToString(123))
XMLAddChild(sMyXMLDoc, "TOTAL", NumToString(420.50))
XMLAddChild(sMyXMLDoc, "VAT", NumToString(19.6))
XMLAddChild(sMyXMLDoc, "NOTES", "Information about invoice 123")

// Details of INVOICE lines:
FOR nIndex = 1 TO 5
	XMLAddChild(sMyXMLDoc, "INVOICE_LINE", ...
		NumToString(nIndex), True)
	XMLAddChild(sMyXMLDoc, "DESCRIPTION", ...
		"Description line " + nIndex)
	XMLAddChild(sMyXMLDoc, "AMOUNT", NumToString(84.25))
	// Moves one level up for the next line (or for the rest)
	XMLParent(sMyXMLDoc) 
END

// Invoice 2
// -----------
XMLParent(sMyXMLDoc) 

// Moves one level up to be on the same level as the previous invoice
// OR XMLRoot(sMyXMLDoc) because there is only one level
XMLAddChild(sMyXMLDoc, "INVOICE", "", True)  // "Parent" node
XMLAddChild(sMyXMLDoc, "NUMBER", NumToString(456))

// Information about the invoice
XMLAddChild(sMyXMLDoc, "TOTAL", NumToString(420.50))
XMLAddChild(sMyXMLDoc, "VAT", NumToString(5.5))
XMLAddChild(sMyXMLDoc, "NOTES", "Information about invoice 456")

// Details of INVOICE lines:
FOR nIndex = 1 TO 10
	// Saves the current position
	nXMLPosition = XMLSavePosition(sMyXMLDoc)
	XMLAddChild(sMyXMLDoc, "INVOICE_LINE", ...
		NumToString(nIndex), True)
	XMLAddChild(sMyXMLDoc, "DESCRIPTION", "Description line " + nIndex)
	XMLAddChild(sMyXMLDoc, "AMOUNT", NumToString(42.5))
	// Other method in relation to XMLParent
	// Restores the position for the next line (or for the rest)
	XMLRestorePosition(sMyXMLDoc, nXMLPosition)
END

sXMLInfo = XMLBuildString(sMyXMLDoc)  // Retrieves the XML that was built
XMLClose(sMyXMLDoc) // Frees the XML document

// Create the XML file
fSaveText(CompleteDir(fExeDir()) + "Invoice.xml", sXMLInfo)
// Display in the application associated with XML
ShellExecute(CompleteDir(fExeDir()) + "Invoice.xml")
Finding the invoices in an XML file
WINDEVJavaUser code (UMC)
This example is used to read and find an invoice in an XML file (Invoice/Invoice Line).
sMonDocXML is string = "XML"
sInfoXML is string 

// Chargement du fichier XML dans une chaîne
sInfoXML = fLoadText(CompleteDir(fExeDir()) + "Facture.xml")

// Initialisation des fonctions XML sur ce fichier
XMLDocument(sMonDocXML, sInfoXML) 

// on se positionne sur la racine
XMLRoot(sMonDocXML)

// Lecture en recherchant les factures et les infos 
//  de ces factures (structure du fichier XML connue)
LOOP
	// Recherche d'une "Facture" dans les éléments et/ou sous éléments
	XMLFind(sMonDocXML, "FACTURE", XMLElement + ...
			XMLChildItem + XMLContinue, ...
			XMLIgnoreCase + XMLExact) 
	IF XMLFound(sMonDocXML) = False THEN BREAK
	// On a trouvé une facture
	// Informations sur la facture
	XMLChild(sMonDocXML)
	XMLFirst(sMonDocXML)
	WHILE NOT XMLOut(sMonDocXML)
		SWITCH XMLElementName(sMonDocXML)
			CASE "NUMERO"
				Trace("Facture n° " + XMLData(sMonDocXML))
			CASE "TOTAL"
				Trace("Montant facture : " + ...
					XMLData(sMonDocXML))
			CASE "TVA"
				Trace("TVA " + XMLData(sMonDocXML))
			CASE "NOTES"
				Trace("Notes : " + XMLData(sMonDocXML))
			CASE "LIGNE_FACTURE"
				// Détail des lignes de la facture en cours
				// Informations sur une ligne de la facture
				XMLChild(sMonDocXML)
				XMLFirst(sMonDocXML)
				WHILE NOT XMLOut(sMonDocXML)
					SWITCH XMLElementName(sMonDocXML)
					CASE "NUMERO"
					Trace("Facture n° " + ...
						XMLData(sMonDocXML))
					CASE "MONTANT"
					Trace("Montant ligne : " + ...
						XMLData(sMonDocXML))
					CASE "DESCRIPTION"
					Trace("Description ligne : " + ...
						XMLData(sMonDocXML))
					OTHER CASE
					// Ne récupère pas 
					// les autres infos de la ligne
					// Remonte au détail facture
					XMLParent(sMonDocXML)
					END
					XMLNext(sMonDocXML)
				END
				// Remonte au niveau du détail de la facture
				XMLParent(sMonDocXML)
		  	OTHER CASE
			// Ne récupère pas les autres infos de la facture
		 END
		 XMLNext(sMonDocXML)
	END
	// On remonte au niveau de la facture
	XMLParent(sMonDocXML)
END
// Annule la recherche 
// pour les autres éventuelles fonctions XML utilisées par la suite
XMLCancelSearch(sMonDocXML)
XMLClose(sMonDocXML) // Libère le document XML
Info("Parcours des factures terminé")
Displaying the content of an XML document
WINDEVUser code (UMC)
The following example is used to display an XML document in a trace with its tree structure. This example corresponds to a procedure that can be called at any time. This procedure has the following syntax: ParcoursXML(<sDocXML> [, <nNiveau>]) where:
  • <sXMLDoc> corresponds to the XML document to use (initialized by XMLDocument)
  • <nLevel> (0 by default) corresponds to the level in the XML tree structure.
sDocXML is string = "DocXML"
sXML is string = fLoadText("C:\MonFichier.xml")
// Initialise le document XML
IF XMLDocument(sDocXML, sXML) = False THEN
	// Document non valide
	Error()
END
// On se positionne sur la racine
XMLRoot(sDocXML)
// On parcourt le document
ParcoursXML(sDocXML)
// On termine le document
XMLClose(sDocXML)
PROCEDURE ParcoursXML(sDocXML, nNiveau=0)
nPositionXML is int
sInfoTrace is string
bBalise is boolean
sDonnées is string
// Parcours de tous les éléments
XMLFirst(sDocXML)
WHILE NOT XMLOut(sDocXML)
	// Récupère les données de l'élément
	sDonnées = XMLData(sDocXML)
	// De quel type d'éléments s'agit-il ?
	SWITCH XMLElementType(sDocXML)
		 CASE XMLAttribute 
			  // Attribut affiché entre côtes
			  sInfoTrace = RepeatString("-", 2*nNiveau) + ...
				"'" + XMLElementName(sDocXML) + "'"
		 CASE XMLTag  
			  // Attribut affiché entre < et >
			  sInfoTrace = RepeatString("-", 2*nNiveau) + ...
				"<" + XMLElementName(sDocXML) + ">"
			  bBalise = True
		 OTHER CASE // dont CAS XMLErreur
			  // Rien
			 sInfoTrace = "Erreur XML"
	END 
	// Il y a des données pour cet élément ?
	IF StringFormat(sDonnées, ccIgnorePunctuationAndSpace + ...
			ccIgnoreSpace + ccIgnoreAccent) = "" THEN
		// Pas de données donc affichage de nom de l'élément seul
		Trace(sInfoTrace)
	ELSE
	  	// Des données
		// Affichage du nom de l'élément avec sa valeur
	  	Trace(sInfoTrace + " = " + sDonnées)
	END
	// Il y a des balises ou des attributs pour cet élément ?
	IF XMLChildExist(sDocXML, XMLTag + XMLAttribute) = True THEN
		// Sauve la position actuelle
		nPositionXML = XMLSavePosition(sDocXML)
		// Se positionne sur le fils
		XMLChild(sDocXML)
		// Parcourt le fils
		ParcoursXML(sDocXML, nNiveau+1)
		// Se repositionne comme avant le positionnement sur le fils
		XMLRestorePosition(sDocXML, nPositionXML, XMLRPDefault)
  	END
  	// Si c'était une balise, on affiche la "balise fermante"
  	IF bBalise = True THEN Trace(sInfoTrace)
 	// Suite du XML
 	XMLNext(sDocXML)
END
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 04/06/2025

Send a report | Local help