How To Convert FoxPro Cursor into XML Data Format

This article was previously published under Q191758
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes how to create an Extensible Markup Language (XML)data file from a cursor in Visual FoxPro 6.0.
Extensible Markup Language (XML) is the universal format for data on theWeb. XML is a text-based format that lets developers describe, deliver andexchange structured data between a range of applications to clients forlocal display and manipulation. XML does not replace HTML, rather, it is acomplementary format.

To create an XML file from a filtered .dbf table follow these steps:
  1. Create a program (.prg) file named Callxml.prg and place the following code in the file:
    LOCAL lcFileName,lcXMLFile,lcFilterlcDBFName = HOME(2) + "data\orders.dbf"lcXMLFile = LOWER(SYS(2023)) + "\orders.xml"lcFilter = "FOR To_Region='WA'"STRTOFILE(DBF2XML(lcDBFName, lcFilter, .T.), ;lcXMLFile)MODIFY FILE (lcXMLFile) NOWAITRETURN						
  2. Create another .prg file named Dbf2xml.prg, and put the following code into this .prg file:
    * Dbf2xml.prg*-- ASCII codes#DEFINE TAB     CHR(9)#DEFINE LF      CHR(10)#DEFINE CR      CHR(13)#DEFINE CR_LF   CR+LFLPARAMETERS tcTable, tcScope, tnTabIndentsLOCAL lcTable, lcAlias, lcScope, lcDBFName, lcIndentText, lcDBF, lcRootNameLOCAL lcValue, lcField, lnFieldCount, lnCount, lnLastSelect, lnLastRecNoLOCAL lcXMLTextDIMENSION laFields[1]IF NOT INLIST(VARTYPE(tcTable), "C", "L") OR NOT ;       INLIST(VARTYPE(tcScope), "C", "L")   RETURN ""ENDIFlcIndentText = IIF(VARTYPE(tnTabIndents) == "N", ;                   REPLICATE(TAB, tnTabIndents), "")lcTable = LOWER(IIF(EMPTY(tcTable), ALIAS(), ALLTRIM(tcTable)))lnLastSelect=SELECT()IF "." $ lcTable   lcDBF = lcTable   IF NOT FILE(lcDBF)      RETURN ""   ENDIF      SELECT 0   lcAlias = LOWER(SYS(2015))   USE (lcDBF) ALIAS (lcAlias) AGAIN SHARED   lcDBFName = LOWER(FORCEEXT(JUSTFNAME(DBF()), ""))ELSE   lcDBF = ""   lcAlias = lcTable   lcDBFName = lcAliasENDIFIF NOT USED(lcAlias)   SELECT (lnLastSelect)   RETURN ""ENDIFlcScope = IIF(EMPTY(tcScope), "ALL", ALLTRIM(tcScope))SELECT (lcAlias)lnLastRecNo = IIF(EOF(), 0, RECNO())lcXMLText = "<"+ lcDBFName + "_table>" + CR_LFlnFieldCount = AFIELDS(laFields)IF lnFieldCount = 0   SELECT (lnLastSelect)   RETURN ""ENDIFlcRootName = lcDBFNameDO WHILE TYPE(lcRootName) != "U"   lcRootName = lcRootName + "1"ENDDOSCAN &lcScope   lcXMLText = lcXMLText + lcIndentText + ;      "<" + lcRootName + ">" + CR_LF         FOR lnCount = 1 TO lnFieldCount      lcField = LOWER(laFields[lnCount, 1])      lcValue = ALLTRIM(TRANSFORM(EVALUATE(lcField)))      IF EMPTY(lcValue)         LOOP      ENDIF            lcXMLText=lcXMLText + lcIndentText + TAB + ;         "<" + lcField + ">" + lcValue + "</" + lcField + ">" + CR_LF   ENDFOR      lcXMLText = lcXMLText + lcIndentText + ;      "</" + lcRootName + ">" + CR_LFENDSCANIF EMPTY(lcDBF)   IF lnLastRecNo > 0      GO lnLastRecNo   ENDIFELSE    USEENDIFSELECT (lnLastSelect)lcXMLText = lcXMLText + CR_LF + "</"+ lcDBFName + "_table>"RETURN lcXMLText					
  3. In the Visual FoxPro Command window, run the Callxml.prg file. Thisresults in the creation of a file named Orders.xml, which is a text filesubset of the Orders.dbf table. The program places the file in the folder pointed to by the operating system environment SET TEMP setting.
For more information about XML, please see the following Microsoft Web site:

Article ID: 191758 - Last Review: 12/05/2015 09:10:43 - Revision: 1.1

Microsoft Visual FoxPro 6.0 Professional Edition

  • kbnosurvey kbarchive kbhowto KB191758