PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • Example for handling an XML file
  • Finding the invoices in an XML file
  • Displaying the content of an XML document
XMLDocument (Example)
Example for handling an XML file
WINDEVWEBDEV - Server codeReports and QueriesWindows MobileJavaPHPUser 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
nSubscript 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 nSubscript = 1 TO 5
XMLAddChild(sMyXMLDoc, "INVOICE_LINE", ...
NumToString(nSubscript), True)
XMLAddChild(sMyXMLDoc, "DESCRIPTION", ...
"Description line " + nSubscript)
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 nSubscript = 1 TO 10
// Saves the current position
nXMLPosition = XMLSavePosition(sMyXMLDoc)
XMLAddChild(sMyXMLDoc, "INVOICE_LINE", ...
NumToString(nSubscript), True)
XMLAddChild(sMyXMLDoc, "DESCRIPTION", "Description line " + nSubscript)
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
WINDEVWEBDEV - Server codeReports and QueriesWindows MobileJavaPHPUser code (UMC)
This example is used to read and find an invoice in an XML file (Invoice/Invoice Line).
sMyXMLDoc is string = "XML"
sXMLInfo is string

// Load the XML file in a string
sXMLInfo = fLoadText(CompleteDir(fExeDir()) + "Invoice.xml")

// Initialize the XML functions on this file
XMLDocument(sMyXMLDoc, sXMLInfo)

// position on the root
XMLRoot(sMyXMLDoc)

// Read while finding the invoices and the information
// about these invoices (structure of XML file known)
LOOP
// Find an "Invoice" in the elements and/or sub-elements
XMLFind(sMyXMLDoc, "INVOICE", XMLElement + ...
XMLChildItem + XMLContinue, ...
XMLIgnoreCase + XMLExact)
IF XMLFound(sMyXMLDoc) = False THEN BREAK
// An invoice was found
// Information about the invoice
XMLChild(sMyXMLDoc)
XMLFirst(sMyXMLDoc)
WHILE not XMLOut(sMyXMLDoc)
SWITCH XMLElementName(sMyXMLDoc)
CASE "NUMBER"
Trace("Invoice # " + XMLData(sMyXMLDoc))
CASE "TOTAL"
Trace("Invoice amount: " + ...
XMLData(sMyXMLDoc))
CASE "VAT"
Trace("VAT " + XMLData(sMyXMLDoc))
CASE "NOTES"
Trace("Notes: " + XMLData(sMyXMLDoc))
CASE "INVOICE_LINE"
// Details of lines for the current invoice
// Information about an invoice line
XMLChild(sMyXMLDoc)
XMLFirst(sMyXMLDoc)
WHILE not XMLOut(sMyXMLDoc)
SWITCH XMLElementName(sMyXMLDoc)
CASE "NUMBER"
Trace("Invoice # " + ...
XMLData(sMyXMLDoc))
CASE "AMOUNT"
Trace("Line amout: " + ...
XMLData(sMyXMLDoc))
CASE "DESCRIPTION"
Trace("Line description: " + ...
XMLData(sMyXMLDoc))
OTHER CASE
// Don't retrieve
// the other info about the line
// Go back to the invoice details
XMLParent(sMyXMLDoc)
END
XMLNext(sMyXMLDoc)
END
// Moves up to the level of invoice details
XMLParent(sMyXMLDoc)
  OTHER CASE
// Don't retrieve the other invoice information
 END
 XMLNext(sMyXMLDoc)
END
// Move up to the invoice level
XMLParent(sMyXMLDoc)
END
// Cancels the search
// for the other possible XML functions used thereafter
XMLCancelSearch(sMyXMLDoc)
XMLClose(sMyXMLDoc) // Frees the XML document
Info("Browse of invoices completed")
Displaying the content of an XML document
WINDEVWEBDEV - Server codeReports and QueriesWindows MobileUser 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 is using the following syntax: XMLBrowse(<sXMLDoc> [, <nLevel>]) 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.
sXMLDoc is string = "XMLDoc"
sXML is string = fLoadText("C:\MyFile.xml")
// Initializes the XML document
IF XMLDocument(sXMLDoc, sXML) = False THEN
// Invalid document
Error()
END
// Position on the root
XMLRoot(sXMLDoc)
// Browse the document
XMLBrowse(sXMLDoc)
// End the document
XMLClose(sXMLDoc)
PROCEDURE XMLBrowse(sXMLDoc, nLevel=0)
nXMLPosition is int
sTraceInfo is string
bTag is boolean
sData is string
// Browse all the elements
XMLFirst(sXMLDoc)
WHILE NOT XMLOut(sXMLDoc)
// Retrieves the element data
sData = XMLData(sXMLDoc)
// What type of element is it?
SWITCH XMLElementType(sXMLDoc)
 CASE XMLAttribute
  // Attribute displayed between quotes
  sTraceInfo = RepeatString("-", 2*nLevel) + ...
"'" + XMLElementName(sXMLDoc) + ""
 CASE XMLTag  
  // Attribute displayed between < and >
  sTraceInfo = RepeatString("-", 2*nLevel) + ...
"<" + XMLElementName(sXMLDoc) + ">"
  bTag = True
 OTHER CASE // including CASE XMLError
  // Nothing
 sTraceInfo = "XML error"
END
// Is there data for this element?
IF StringFormat(sData, ccIgnorePonctuationAndSpace + ...
ccIgnoreSpace + ccIgnoreAccent) = "" THEN
// No data therefore display the element name only
Trace(sTraceInfo)
ELSE
  // Data
// Display the name of the element and its value
  Trace(sTraceInfo + " = " + sData)
END
// Are there tags or attributes for this element?
IF XMLChildExist(sXMLDoc, XMLTag + XMLAttribute) = True THEN
// Saves the current position
nXMLPosition = XMLSavePosition(sXMLDoc)
// Positions on the child
XMLChild(sXMLDoc)
// Browses the child
XMLBrowse(sXMLDoc, nLevel+1)
// Repositions like just before positioning onto the child
XMLRestorePosition(sXMLDoc, nXMLPosition, XMLRPDefault)
  END
  // If it was a tag, display the "closing tag"
  IF bTag = True THEN Trace(sTraceInfo)
// Rest of XML
XMLNext(sXMLDoc)
END
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment