Condividi tramite


CURSORTOXML( ) Function

Converts a Visual FoxPro cursor to XML.

CURSORTOXML(nWorkArea | cTableAlias, cOutput [, nOutputFormat 
[, nFlags [, nRecords [, cSchemaName [, cSchemaLocation [, cNameSpace ]]]]]])

Parameters

  • nWorkArea
    Specifies the work area of the table from which to create the XML string.

    If you specify 0 or no value, Visual FoxPro uses the current work area.

  • cTableAlias
    Specifies the alias of the table from which to create the XML string.

  • cOutput
    Specifies the path and file name or memory variable name to which results are sent.

    If nFlags is set to 0 (default) for memory variable output, XML is returned to the memory variable. If the memory variable does not exist, it is created.

    If nFlags is set to 512 for file output, and the file does not exist, it is created. If the file already exists, it is overwritten. The SAFETY setting is observed.

  • nOutputFormat
    Specifies the output format of the XML string. The following table lists the values for nOutputFormat.

    nOuputFormat Description
    1 – ELEMENTS Element-centric XML (Default)
    2 – ATTRIBUTES Attribute-centric XML
    3 – RAW Generic, attribute-centric XML
  • nFlags
    Specifies the formatting of the XML that is produced and its destination. The following table lists the values for nFlags.

    nFlags Bit Output description
    0 0000 Produce XML in UTF-8 format. (Default)

    This setting creates a memory variable if one does not exist when specified by cOutput and returns XML to the memory variable.

    The XML Declaration does not contain an Encoding= attribute, that is, no encoding attribute set to UTF-8.

    1 0001 Produce unformatted XML as a continuous string.
    2 0010 Enclose empty elements with open and closing elements, for example, <cc04><cc04/>.
    4 0100 Preserve white space in fields.
    8 1000 Wrap Memo fields in CDATA sections.
    16 10000 Output encoding. Output is set to the cursor code page.

    To ensure accurate character translation, the Visual FoxPro default code page must match the code page of the cursor. You can accomplish this by setting character and memo fields in the cursor to NOCPTRAN (character binary/memo binary).

    When setting this value with tables using any of the code pages, the encoding attribute in the XML is set to an empty string (""). To change to the correct encoding attribute, use the STRTRAN( ) function. For example, for code page 936, provide the following to the resulting XML string:

    strxml=STRTRAN(strxml, 'encoding=""', 'encoding="gb2312"'
    32 100000 Output encoding
    512 1000000000 Output to the file specified by cOutput.

    If a file does not exist, it is created. If the file already exists, it is overwritten. The SAFETY setting is observed.

    4096 1000000000000 Disables base64 encoding.

    CURSORTOXML( ) exports Memo (Binary) fields as xsd:base64binary unless you use nFlags set to 4096. In Visual FoxPro, base64 encoding is meant for encoding only binary data.

    The following table describes how the encoding attribute is written when output encoding defaults to the cursor or table code page.

    Note   Encoding flags are set by combining bits 4 and 5 (0010000).

    Encoding flag Bits 4 and 5 Description
    +0 00 Windows-1252 (Default)
    +16 01 Set output encoding attribute to the cursor code page.
    +32 10 Set output encoding attribute to UTF-8 with no character translation.
    +48 11 Set output encoding attribute to UTF-8 and translate character data to UTF-8.

    The following table lists common Windows-compatible code pages.

    Code page Platform Encoding attribute in XML Declaration Comments
    437 MS-DOS, US ibm437  
    850 MS-DOS, International ibm850  
    865 MS-DOS, Nordic Empty string ("")  
    866 MS-DOS, Russian cp866  
    932 Windows, Japanese shift-jis  
    936 Traditional Chinese gb2312  
    949 Windows, Korean iso-2022-kr or:

    ks_c_5601-1987

    950 Windows, Chinese (Taiwan) big5  
    1250 Windows, East European Windows-1250 note case
    1251 Windows, Russian Windows-1251  
    1252 Windows, U.S., West European Windows-1252  
    1253 Windows, Greek Windows-1253  
    1254 Windows, Turkish Windows-1254  
    1255 Windows, Hebrew Windows-1255  
    1256 Windows, Arabic Windows-1256  

    Note   Visual FoxPro uses Windows-1252 as the default encoding. You can specify that encoding attribute be set to match the code page, such as Big5 (code page 950), when using double-byte character sets (DBCS) for DBCS languages that Visual FoxPro supports code pages for. When using Windows-1252 and DBCS, no additional character translation is required to display correctly in Internet Explorer.

    However, to make XML more compliant for Web browsers that can interpret UTF-8, you can optionally set the encoding attribute to UTF-8 instead of Windows-1252. The result set undergoes translation to UTF-8 characters only when you set nFlags to 48 to specify the encoding attribute as UTF-8 and translate character data to UTF-8 format. This is required only when the data actually contains double-byte characters. However, you do not have to use character translation, for example, if you are outputting only latin (sgl byte) characters.

  • nRecords
    Specifies the number of records to output to XML and has a default value is 0.

    If nRecords is 0, all records are output. If nRecords is greater than the number of records remaining in the table, all remaining records are output.

  • cSchemaName
    Specifies the name and location of the schema information for the data in cFileName.

    The following table lists the values for cSchemaName.

    cSchemaName Description
    "" Specifies that no schema is produced.
    "1" Specifies that inline schema is produced.
    <cSchemaName> Specifies the name and path of the external file to contain the schema (scoped to the root element of the XML)

    If cSchemaLocation is not provided or is blank, and cSchemaName contains a file name, the contents of cSchemaName are written to the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute in the XML.

    For example, the following code shows how Visual FoxPro generates a generic XML file named myxmlfile.xml from the labels.dbf file in the "Labels" alias and the schema file named mySchema in the same folder.

    CURSORTOXML("LABELS", "myXMLFile.xml", 1, 512, 0,  "mySchema.xsd")
    

    If you specify an empty string ("") instead of "mySchema.xsd", no schema is produced. In the following code, specifying "1" produces an inline schema:

    CURSORTOXML("LABELS", "myXMLFile.xml", 1, 512, 0, "1")
    

    If cSchemaName includes a URI, the schema is written to the current directory and must be uploaded to the server to be accessed by the browser or parser. External schema always are written to the same location as the XML file.

  • cSchemaLocation
    Specifies an optional location where the application reading the XML data should look for the schema file. Use this parameter only when you are deploying your schema to a location other than the location of the XML data.

    The contents of cSchemaLocation are written to the xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute of the XML data produced. The cschemaLocation parameter can be an HTTP address or other URI. You need to copy the schema file to the location you have specified in cSchemaLocation.

    The following example produces XML data:

       CURSORTOXML("LABELS", "myXMLFile.xml", 1, 512, 0, ;
           "mySchema.xsd", "https://www.microsoft.com/mySchema.xsd")
    

    containing the following attribute:

       xsi:noNamespaceSchemaLocation=" https://www.microsoft.com/mySchema.xsd"
    

    Specifying cSchemaLocation when cSchemaName is blank causes the same attributes to be written to the XML data. This makes it possible for you to point to an existing schema without recreating the schema each time CURSORTOXML( ) is called.

  • cNamespace
    Specifies the namespace of the XML or schema to be produced and has an empty string ("") as the default value.

    Specifying a value for cNameSpace sets the targetNamespace attribute to the same value and adds the elementFormDefault="qualified" attribute to the schema.

    If you do not specify a cNamespace value, and the schema is external, no namespace declaration is written to the schema. If do not specify a cNamespace value, and the schema is inline, the targetNamespace in the schema is set to an empty string ("").

Return Values

Numeric data type. CURSORTOXML( ) returns the number of bytes written to the file or a memory variable.

Remarks

CURSORTOXML( ) output adheres to the cursor index order, SET FIELDS TO, and current filter settings. This function does not preserve the cursor location. After the call, the cursor record pointer reports EOF if all records are output or points to the last record output to XML if not all records were output.

The resulting XML from CURSORTOXML( ) contains the same scale as the exported table if the table contains Double data type values. For example, if a Double column is created with a scale of 6 using the following code:

CREATE TABLE test (col1 b(6))

The resulting XML contains six digits to the right of the decimal point.

When you use the CURSORTOXML( ) function to export a table or cursor to XML, the root node is always called "VFPData", regardless of the output format.

For Date types only, you can export "EMPTY" Date and DateTime types using CURSORTOXML( ). However, the XML schema validation might fail because "EMPTY" is not valid for these types in XML Schema Definition (XSD) schema. This is an issue only if schema is required, and the resulting XML is validated against an XML parser that can interpret XSD schemas. As a workaround, you might need to change the data to a nonempty representation by calling the appropriate SELECT statement and use CURSORTOXML( ) on the created cursor. For example, you can change the empty Date or DateTime values to .NULL.:

SELECT orderid, EVL(shippeddate,.NULL.) as ShippedDate FROM orders

You can also change the empty Date or DateTime to an appropriate representation for "empty":

SELECT orderid, IIF(EMPTY(shippeddate),{^1899-12-30 00:00:00},tc11); 
   as ShippedDate FROM orders

See Also

XML Functions | XMLUpdateGram( ) Function | XMLTOCURSOR( ) Function