ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / XML file functions
  • Usage example of the XMLOpenReader function
XMLOpenReader (Example)
Usage example of the XMLOpenReader function
This example imports a large XML file into a database table.
// Syntax:
//[ <Result> = ] ImportXML (<sNameXMLFileToImport> is string,
// <sNameDestinationTable> is string)
//
// Parameters:
// sNameXMLFileToImport (ANSI string): Name of XML file to import
// (a file with same type as XML files created by the HExportXML function)
// sNameDestinationTable (ANSI string): Name of destination file
// Return value:
// boolean: True if the import was successfully performed
//
// Example:
// ImportXML("C:\Temp\FileToImport.xml","Command")
//
PROCEDURE ImportXML(sNameXMLFileToImport is string,...
sNameDestinationTable is string): boolean
 
// Example of processed XML file:
// <?xml version="1.0" encoding="ISO-8859-1" stand-alone="yes"?>
// <?xml-stylesheet href="FichierAImporter.xsl" type="text/xsl"?>
//  <HF_DOCUMENT>
// <TableName>
// <ItemName>ItemValue</ItemName>
// ...
// <NameIndexItem>
// <NameIndexItem_1>ValueIndexItem</NameIndexItem_1>
// ...
// </NameIndexItem>
// ....
// </TableName>
// ....
//  </HF_DOCUMENT>
 
// xmRecord is xmlReader
xmlDocRoot is xmlReader = XMLOpenReader(sNameXMLFileToImport)
IF ErrorOccurred THEN
// Error while declaring the file, display the error
Error("Import failure",ErrorInfo())
RESULT False
END
 
ToastDisplay("Import in progress...")
//Browse file
sImportErrors is string
sFileName is string
sItemName is string
sItemValue is string
nIndex is int
nNbImport is unsigned 8-byte int
//browses the first-level elements of XML
FOR EACH xmlDocRoot
// Progress bar
// PROGBAR_Progress++
// Start tag whose name is "HF_DOCUMENT"?
IF xmlDocRoot.Type = XMLReaderStartTag _AND_ xmlDocRoot.Name = "HF_DOCUMENT" THEN
// Browse all sub-elements (the records)
FOR EACH xmlRecord OF xmlDocRoot
// Reset the items of destination file
HReset(sNameDestinationTable)
// Start tag for the requested file?
IF xmlRecord.Type = XMLReaderStartTag _AND_  ...
xmlRecord.Name ~= sNameDestinationTable THEN
sFileName=xmlRrecord.Name
// Browse the sub-elements (the items)
FOR EACH xmlItem OF xmlRecord
SWITCH xmlItem.Type
CASE XMLReaderStartTag
sItemName = xmlItem..Name
nIndex = 0
// Browse the sub-elements (the item values)
FOR EACH xmlItemValue OF xmlItem
// "A priori", it is not an indexed item
IF nIndex=0 _AND_ ...
xmlItemValue.Type = XMLReaderText THEN
// Get the value
sItemValue = xmlItemValue.Value
ELSE
// Is it a start tag?
IF xmlItemValue.Type = XMLReaderStartTag THEN
// Therefore, it is an indexed item
// Browse the sub-elements
// (the indexed item values)
FOR EACH xmlIndexedItemValue OF xmlItemValue
// it's ok
SI xmlIndexedItemValue.Type = ...
XMLReaderText THEN
// Value of the next index
nIndex++
sItemValue = xmlItemValue.Value
// Assign the indexed item
WHEN EXCEPTION IN
{sNameDestinationTable + "." + ...
sItemName, subItem}[nIndex]= ...
sItemValue
DO
// nothing: it's to avoid an error
// if the item does not exist
// in the destination file
END
END
END
END
END
END
// was it an indexed item?
IF nIndex = 0 THEN
// no therefore
WHEN EXCEPTION IN
{sNameDestinationTable+"."+ ...
sItemName, subItem}=sItemValue
DO
// nothing: it's to avoid an error
// if the item does not exist in the destination file
END
// ELSE
// Indexed item, no action required
END
 
OTHER CASE
// Nothing
END
END  // FOR EACH xmlItem
// End of file record
// Add into the destination file
IF NOT HAdd(sNameDestinationTable) THEN
sImportErrors += HErrorInfo()
ELSE
nNbImport++
END
END
END // FOR EACH xmlRecord
END
END
 
// Errors during the import?
IF sImportErrors <> "" THEN
Error("Errors occurred during the import: ", sImportErrors)
RESULT False
ELSE
ToastDisplay("Import of "+nNbImport + " records successfully ended without error")
RETURN True
END
Minimum version required
  • Version 23
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 05/26/2022

Send a report | Local help