ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / XML file functions
  • Overview
  • Example for reading an XML document
  • Example for creating an XML document
  • Example for modifying an XML document
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
Overview
The following examples is used to handle an XML document via the xmlDocument and cmlNode variables, ...
Example for reading an XML document
NameXMLFile is string
NameXMLFile = fSelect("*.*", "File.xml", "Select an XML file", ...
"XML file (*.xml)" + TAB + "*.xml" + CR + "All" + TAB + "*.*")
 
// Checks whether the file is selected
IF NameXMLFile <> "" THEN
// Open the XML file
xmlDoc is xmlDocument
xmlDoc = XMLOpen(NameXMLFile, fromFile)
// Checks whether at least one root is found (a single one is usually found)
IF xmlDoc..RootNode..Occurrence < 1 THEN
Info("The XML file " + NameXMLFile + " contains no root node")
ELSE
// Displays the details of all the roots
FOR EACH ARootNode OF xmlDoc..RootNode
DetailsNodeAndSubNodes(ARootNode)
END
END
END
// Code of the DetailsNodeAndSubNodes procedure
 
// Summary: Displays in a trace window the tree structure of the node passed as parameter
// Syntax:
// DetailsNodeAndSubNodes(<anXMLNode> is xmlNode [, <nLevel> is int])
//
// Parameters:
// anXMLNode (xmlNode): XML node whose details must be displayed
// nLevel (integer - default value=0): Current level of the node
// Return value:
// None
PROCEDURE DetailsNodeAndSubNodes(LOCAL anXMLNode is xmlNode, nLevel is int=0)
 
sAttributes is string = ""
 
// Checks the existence of the attributes
IF anXMLNode..Attribute..Occurrence > 0 THEN
// Yes, therefore browse the attributes
FOR EACH aAttribute OF anXMLNode..Attribute
sAttributes += " " + UnicodeToAnsi(anAttribute..Name) + "=""" + ...
UnicodeToAnsi(anAttribute..Value) + """"
END
END
 
// Beginning of node
Trace(RepeatString("-", 2*nLevel) + "<" + UnicodeToAnsi(anXMLNode..Name) + sAttributes + "> " + ...
UnicodeToAnsi(anXMLNode..Text))
 
// Browse the sub-nodes
FOR EACH anXMLSubNode OF anXMLNode
// The name of the sub-node is not empty?
// (caution, everything is in Unicode with the XML variables)
IF NOT UnicodeToAnsi(anXMLSubNode..Name) ~= "" THEN
// Sub-nodes
DetailNodeAndSubNodes(anXMLSubNode, nLevel+1)
// End of sub-node
END
END
// End of node
Trace(RepeatString("-", 2*nLevel) + "</" + anXMLNode..Name + ">")
Example for creating an XML document
// Create an XML variable
xmlInvoice is xmlDocument,description="Model_Invoice"
// Note: ,description="Model_Invoice" is optional.
// This notation gives access to the assisted input if an example XML file was imported
 
xmlANode is xmlNode
 
// Create the root (because the automatic creation is done on one level only)
xmlANode..Name = "LIST_OF_INVOICES"
Add(xmlInvoice, xmlANode)
 
// ----Create a new invoice
xmlANode..Name = "INVOICE"
Add(xmlInvoice.LIST_OF_INVOICES, xmlANode)
 
// Create the elements of the invoice
xmlInvoice.LIST_OF_INVOICES.INVOICE[1].Number = 1
xmlInvoice.LIST_OF_INVOICES.INVOICE[1].TOTAL = 172.6
xmlInvoice.LIST_OF_INVOICES.INVOICE[1].VAT = 19.6
 
// Create a new invoice line
xmlANode..Name = "INVOICE_LINE"
Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[1], xmlANode)
xmlInvoice.LIST_OF_INVOICES.INVOICE[1].INVOICE_LINE[1].Description = ...
"Description line 1 invoice 1"
xmlInvoice.LIST_OF_INVOICES.INVOICE[1].INVOICE_LINE[1].AMOUNT = 84.25
 
// Create a new invoice line
xmlANode..Name = "INVOICE_LINE"
Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[1], xmlANode)
xmlInvoice.LIST_OF_INVOICES.INVOICE[1].INVOICE_LINE[2].Description = ...
"Description line 2 invoice 1"
xmlInvoice.LIST_OF_INVOICES.INVOICE[1].INVOICE_LINE[2].AMOUNT = 88.35
 
//----Create a new invoice
xmlANode..Name = "INVOICE"
Add(xmlInvoice.LIST_OF_INVOICES, xmlANode)
 
// Create the elements of the invoice
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].Number = 2
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].TOTAL = 225.65
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].VAT = 5.5
 
// --Create a new invoice line
xmlANode..Name = "INVOICE_LINE"
Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[2], xmlANode)
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[1].Description = ...
"Description line 1 invoice 2"
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[1].AMOUNT = 52.35
 
// --Create a new invoice line
xmlANode..Name = "INVOICE_LINE"
Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[2], xmlANode)
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[2].Description = ...
"Description line 2 invoice 2"
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[2].AMOUNT = 14.45
 
// --Create a new invoice line
xmlANode..Name="INVOICE_LINE"
Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[2], xmlANode)
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[3].Description = ...
"Description line 3 invoice 2"
xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[3].AMOUNT = 158.85
 
// Result string
Info(XMLBuildString(xmlInvoice))
// or XMLSave(xmlInvoice, SysDir(srMyDocuments) + ["\"] + ...
//            "List_Invoices.xml", XMLDocumentDefault)
Example for modifying an XML document
// Modify the XML document
// Load the XML content
MyXMLDoc is xmlDocument,description="NEWS"
MyXMLDoc = XMLOpen(gsPathXMLFile, fromFile)
IF ErrorOccurred = True THEN
RESULT "Error detected when opening the XML: " + ErrorInfo()
ELSE
Trace("XML file currently processed: " + gsPathXMLFile)
END
 
NodeRSS is xmlNode,description="NEWS.rss.channel.item"
FOR EACH NodeRSS OF  MyXMLDoc.rss.channel ON item
IF NodeRSS.title = sFormer_Title THEN
Trace("The news was found, we are going to modify it")
NodeRSS.title = sNew_Title
NodeRSS.description = sNew_Comment
NodeRSS.link = sNew_Url
END
END
XMLSave(MyXMLDoc, gsPathXMLFile)
 
// Everything is correct
RETURN ""
Minimum version required
  • Version 16
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 05/26/2022

Send a report | Local help