Click or drag to resize

CursorToXML Function

X#
-- todo --
Converts a X# cursor to XML.

Namespace:  XSharp.VFP
Assembly:  XSharp.VFP (in XSharp.VFP.dll) Version: 2.19
Syntax
 FUNCTION CursorToXML(
	uArea,
	cOutput,
	nOutputFormat,
	nFlags,
	nRecords,
	cSchemaName,
	cSchemaLocation,
	cNameSpace
) AS USUAL CLIPPER
Request Example View Source

Parameters

uArea (Optional)
Type: Usual
Specifies the work area number or alias of the table from which to create the XML string. If you specify 0 or no value, X# uses the current work area.
cOutput (Optional)
Type: Usual
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 setting for Set SAFETY is observed.
nOutputFormat (Optional)
Type: Usual
Specifies the output format of the XML string. The table in the remarks section lists the values for nOutputFormat.
nFlags (Optional)
Type: Usual
Specifies the formatting of the XML that is produced and its destination. The table in the remarks section lists the values for nFlags.
nRecords (Optional)
Type: Usual
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 (Optional)
Type: Usual
Specifies the name and location to contain schema information for the data in cOutput, for example "MySchema.xsd". If no extension is provided, the schema file is created with an .xsd extension.
The table in the remarks section lists the values for cSchemaName.
cSchemaLocation (Optional)
Type: Usual
Specifies an optional location where the application reading the XML data should look for the schema file.
Note Note
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 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:
X#
1CURSORTOXML("LABELS", "myXMLFile.xml", 1, 512, 0, ;
2"mySchema.xsd", "http://www.microsoft.com/mySchema.xsd")
containing the following attribute:
X#
1xsi:noNamespaceSchemaLocation=" http://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 (Optional)
Type: Usual

Return Value

Type: Usual
Numeric data type. CURSORTOXML( ) returns the number of bytes written to the file or a memory variable.
Remarks
You can use CURSORTOXML( ) with the OLE DB Provider for X#. However, the _VFPVFPXMLProgID property is not supported because the _VFP system variable is not supported in the OLE DB Provider.
To use the X# OLE DB Provider with CURSORTOXML( ), you must install MSXML 3.0 on the computer with the OLE DB Provider.
Note Note
CURSORTOXML( ) output adheres to the cursor index order, Set Fields TO, and current filter settings.
However, it does not preserve the cursor location. After the calling CURSORTOXML( ), if all records are output, the cursor record pointer reports EOF. If not all records are output, it points to the last record output to XML.
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 codem the resulting XML contains six digits to the right of the decimal point:
X#
1Create Table test (col1 b(6))
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 an XML schema definition (XSD) schema. This is an issue only if a schema is required, and the resulting XML is validated against an XML parser that can interpret XSD schemas. To work around this issue, 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.:
X#
1Select orderid, EVL(shippeddate,.NULL.) as ShippedDate From orders
You can also change the empty Date or DateTime to an appropriate representation for "empty":
X#
1Select orderid, IIF(EMPTY(shippeddate),{^1899-12-30 00:00:00},tc11);
When using flag 32768, the flags 16 and 32 settings can affect which code page is applied depending on the type of data you are writing to XML. The following tables show the possible combinations of settings and the code page each combination applies. For XML documents written with the 32768 flag set, the following code pages are applied.
Flag 16Flag 32Flag 32768 is Set
Not setNot set XML documents: Window-1252.
Unicode data: code page 1252.
Character data: default code page, unless a field is marked as NOCPTRANS.
True (.T)Not set XML documents: Plus-CodePage property of the cursor.
If XMLField CodePage property is greater than zero (0) and it doesn’t match the cursor’s code page, an error is reported.
Unicode data: the Code page property of the cursor object.
Character data: none. Raw data from the X# tables (.dbf) are used instead.
Not setSet XML documents: UTF-8 code page.
Unicode data: UTF-8 code page.
Character data: Default code page unless field is marked as NOCPTRANS, in which case no additional character translation to UTF-8 occurs.
SetSet XML documents: UTF-8 code page.
Unicode data: UTF-8 code page.
Character data: default code page unless a field is marked as NOCPTRANS, in which case the data are translated to UTF-8 using the SYS(3005) setting.
For XML documents written without setting the 32768 flag, the following code pages are applied.
Flag 16Flag 32Without the 32768 flag
Not setNot set XML documents: Window-1252.
Unicode data: code page 1252.
Character data: default code page unless a field is marked as NOCPTRANS.
True (.T)Not set XML documents: CodePage property of the cursor.
Character data: none. Raw data from the X# tables (.dbf) are used instead.
Unicode data: code page property of the cursor object.
Not setSet XML documents: UTF-8 code page.
Unicode data: UTF-8 code page.
Character data: default code page unless field is marked as NOCPTRANS, in which case no additional character translation to UTF-8 occurs.
SetSet XML documents: UTF-8 code page.
Unicode data: UTF-8 code page.
Character data: default code page unless field is marked as NOCPTRANS, in which case, they are translated to UTF-8 using code page for the current SYS(3005) setting.
nOutputFormatDescription
1 – ELEMENTS(Default) Element-centric XML
2 – ATTRIBUTESAttribute-centric XML
3 – RAWGeneric, attribute-centric XML
nFlagsBitOutput description
00000 (Default) Produce XML in UTF-8 format.
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 is set to UTF-8.
10001Produce unformatted XML as a continuous string.
20010Enclose empty elements with open and closing elements, for example, <cc04><cc04/>.
40100Preserve white space in fields.
81000Wrap Memo fields in CDATA sections.
1610000 Output encoding. Output is set to the cursor code page.
To ensure accurate character translation, the X# 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:
X#
1strxml=STRTRAN(strxml, 'encoding=""', 'encoding="gb2312"'
32100000Output encoding.
5121000000000 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 setting for Set SAFETY is observed.
40961000000000000 Disables base64 encoding.
CURSORTOXML( ) exports Memo (Binary) fields as xsd:base64binary unless you use nFlags set to 4096.
In X#, base64 encoding is meant for encoding only binary data.
32768noneIndicates that a code page should be used.
The following table describes how the encoding attribute is written when output encoding defaults to the cursor or table code page.
Note Note
Encoding flags are set by combining bits 4 and 5 (0010000).
Encoding flagBits 4 and 5Description
+000(Default) Windows-1252
+1601Set output encoding attribute to the cursor code page.
+3210Set output encoding attribute to UTF-8 with no character translation.
+4811Set output encoding attribute to UTF-8 and translate character data to UTF-8.
The following table lists common Windows-compatible code pages.
Code pagePlatformEncoding attribute in XML Declaration Comments
437MS-DOS, USibm437
850MS-DOS, Internationalibm850
865MS-DOS, NordicEmpty string ("")
866MS-DOS, Russiancp866
932Windows, Japaneseshift-jis
936Simplified Chinesegb2312
949Windows, Koreaniso-2022-kr or:
ks_c_5601-1987
950Windows, Traditional Chinese (Taiwan)big5
1250Windows, East EuropeanWindows-1250Note case.
1251Windows, RussianWindows-1251
1252Windows, U.S., West EuropeanWindows-1252
1253Windows, GreekWindows-1253
1254Windows, TurkishWindows-1254
1255Windows, HebrewWindows-1255
1256Windows, ArabicWindows-1256
Note Note
X# uses Windows-1252 as the default encoding. You can specify that the 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 for which X# supports code pages.
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 (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. You do not have to use character translation, STRCONV ( ), for example, if you are outputting only Latin (single byte) characters.
cSchemaNameDescription
cSchemaName Specifies the name and path of the external file for the schema (scoped to the root element of the XML).
Note Note
If cSchemaName contains a file name and cSchemaLocation is not provided or is blank, the contents of cSchemaName xsi:schemaLocation or xsi:noNamespaceSchemaLocation attribute in the XML.
In the following example, X# 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.
X#
1CURSORTOXML("LABELS", "myXMLFile.xml", 1, 512, 0, "mySchema.xsd")
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 schemas always are written to the same location as the XML file.
"1" Specifies an inline schema is produced. For example, the following code produces an inline schema:
X#
1CURSORTOXML("LABELS", "myXMLFile.xml", 1, 512, 0, "1")
""Specifies that no schema is produced.
See Also