PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
  • Example for handling an XML file
  • Finding the invoices in an XML file
XMLParent (Example)
Example for handling an XML file
WEBDEV - Server codePHP
sMyXMLDoc is string = "XML"
sXMLInfo is string // Result string
nSubscript is int
nXMLPosition is int

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

// 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
// "Parent" node
XMLAddChild(sMyXMLDoc, "INVOICE", "", True)  
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

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

// 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
WEBDEV - Server codePHP
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 XMLOut(sMyXMLDoc) = False
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 XMLOut(sMyXMLDoc) = False
SWITCH XMLElementName(sMyXMLDoc)
CASE "NUMBER"
Trace("Invoice # " + XMLData(sMyXMLDoc))
CASE "AMOUNT"
Trace("Amount of the line: " + ...
XMLData(sMyXMLDoc))
CASE "DESCRIPTION"
Trace("Description of the line: " + ...
XMLData(sMyXMLDoc))
OTHER CASE
// Don't retrieve the other information
// about the line, move up to the level of
// invoice details
XMLParent(sMyXMLDoc)
END
XMLNext(sMyXMLDoc)
END
// Move 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)
// Frees the XML document
XMLClose(sMyXMLDoc)
Info("Browse of invoices completed")
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment