Visual Foxpro 7 new debut of XML in COM component data delivery significance

Source: Internet
Author: User
Tags chr error code execution integer join xml parser

Say in front

I remember the first time I contacted COM component programming, there is the question: there is no good way to pass the dataset. We know that Visual FoxPro is the most powerful data processing system, and its cursor strong, flexible support is unmatched. Since Visual FoxPro is so powerful at data set processing, COM programming, how to simply in various types of client programs and Visual FoxPro written by the COM components between the transfer cursor--only easy to come and go, with the convenience of comfort!

At that time we imitate ADO component, use "attribute + Loop" method solves this problem, remember Fbilo translation of an article has introduced:

Overall, feel that the time "live" very tired, and this solution does not solve the client to COM components to pass the cursor problem!

In my "visual FoxPro ramble" article, I envision using XML to solve this problem and try some of the times in Visual FoxPro 6. I have written a small program called: Cursortoxml to solve the Cursor to the transformation of XML, but for the transformation of XML to Cursor I am very helpless, we know that some large companies such as Microsoft have developed a component to parse XML, logically speaking, as long as the use of these things can complete the XML To the cursor, but the actual operation is very troublesome. Don't believe it, you can try! This period, even if I successfully encapsulated the XML parser, is not very meaningful, because: all the development tools for XML support is not good, my COM component support, the so-called tango!

Suddenly, as the spring Breeze comes

At the beginning of 2000, I heard that xml,2001 had bought a reference book on XML at the beginning of this year, and began to understand what it called "Extensible Markup Language." This article is not a topic of XML discussion, I am not an expert in this field, so I do not waste ink on XML. My understanding of XML is to use a certain canonical format, in the form of text to show the data. text-based, standards-based format, so XML is inherently adaptable, which is why we say that XML is used to pass data sets between various clients and COM-developed by Visual FoxPro!

Microsoft's Visual FoxPro 7 fully supports XML, embodied in three functions:

Cursortoxml (Nworkarea | ctablealias, Coutput [, Noutputformat [, nflags [, Nrecords [, Cschemaname [, CSchemaLocation [, C NameSpace]]]]

Xmltocursor (Xmlsource eexpression | cxmlfile [, ccursorname [, nflags]])

Xmlupdategram ([Caliaslist [, nflags]])

Really have "like the night Spring breeze to" the feeling, one is feeling: industry technology development fast; Two is feeling: our Visual FoxPro also catch up with, timely launch this fashionable and meaningful function!

In general, the Visual FoxPro 7 support for XML is achieved through encapsulation of the MSXML Parser. Remember-Before installing Visual FoxPro 7, Microsoft will be prompted to install first-"MSXML Parser 3.0"!

Cursortoxml () and Xmltocursor ()

Cursortoxml () converts cursor to an XML file or string

Cursortoxml (Nworkarea | ctablealias, Coutput [, Noutputformat [, nflags [, Nrecords [, Cschemaname [, CSchemaLocation [, C NameSpace]]]]

Nworkarea | ctablealias--Specifies the converted Cursor

coutput--Specifies the output file name or variable name

noutputformat--specify the format of the data in the XML document

Here are three options:

A.noutputformat=1:elements

-<employees> <employeeid>1</employeeid> <lastname>Davolio</lastname> <firstname >Nancy</firstname> <title>sales representative</title> <titleofcourtesy>ms.</ Titleofcourtesy> <birthdate>1948-12-08T00:00:00</birthdate>

B.noutputformat=2:attributes

<employees employeeid= " 1"Lastname=" Davolio"Firstname=" Nancy"Title=" Sales Representative"Titleofcourtesy=" Ms."Birthdate=" 1948-12-08t00:00:00"Hiredate=" 1992-05-01t00:00:00"Address=" 507-20th Ave. E. Apt. 2A"City=" Seattle"Region=" WA"Postalcode=" 98122"Country=" USA"Homephone=" (206) 555-9857"Extension=" 5467"Notes=" "reportsto=" 2"Photopath=" bmp/davolio.bmp"/>

C.noutputformat=3:row

<row employeeid= " 1"Lastname=" Davolio"Firstname=" Nancy"Title=" Sales Representative"Titleofcourtesy=" Ms."Birthdate=" 1948-12-08t00:00:00"Hiredate=" 1992-05-01t00:00:00"Address=" 507-20th Ave. E. Apt. 2A"City=" Seattle"Region=" WA"Postalcode=" 98122"Country=" USA"Homephone=" (206) 555-9857"Extension=" 5467"Notes=" "reportsto=" 2"Photopath=" bmp/davolio.bmp"/>

nflags--Specify Tag

This is the most complex parameter, and here's what you should be aware of: if you want to output XML to a file, let Nflags=512+n (n be another tag); If you want to output XML to a memory variable, the nflags is not 512;

Record range for nrecords--output

Generally we will output all the records in cursor, so this parameter is more than 0 generation!

Example:

Output data from the employee of the instance database into C:temp1.xml:

Cursortoxml ("Employees", "C:\temp1.xml", 1,512+16,0, "1")

Output the employee's data from the instance database to the memory variable ABC:

Cursortoxml ("Employees", "ABC", 3,512+16,0, "1")

Xmltocursor () converts an XML file or string into a cursor

Xmltocursor (Xmlsource eexpression | cxmlfile [, ccursorname [, nflags]])

Xmlsource Eexpression | cxmlfile--Specifies the source of the XML, either a file or a memory variable

Ccursorname--The name of the new cursor

Example:

Turn the c:\temp1.xml into a cursor:t1

Xmltocursor ("C:\temp1.xml", "T1")

Transforming Memory variables into CURSOR:T2

Xmltocursor ("C:\temp2.xml", "T2")

The Xmlupdategram () function has nothing to do with what is described in this article and is not covered here.

An instance that is passed by using XML

Writing COM components

Code:

#define CL chr (+CHR) (10)

DEFINE CLASS POWERVFP as session Olepublic
datasession=2

PROCEDURE CREATECUROSR (cXml as string,ccursorname as String)
Xmltocursor (Cxml,ccursorname)

Endproc

PROCEDURE Execfoxcode (Ccode as String)
Execscript (Ccode)
Endproc

FUNCTION getcursor (Ccursorname As String) as String
Local cXml as String
Cursortoxml (Ccursorname, "cXml", 3,48,0, "")
Return CXML
Endfunc

PROCEDURE Error (nerror as integer,cmethod as String, nline as Integer)
Local Ctext as String
ctext=;
' Error time: ' +transform (DateTime ()) +cl+;
' Error code: ' +str (nerror,4) +cl+;
' ERROR hint: ' +message () +cl+;
' Wrong way: ' +cmethod+cl+;
' ERROR line number: ' +transform (nline) +cl+cl
Strtofile (Ctext, ' c:\PowerFox.txt ',. T.)
Comreturnerror ("Powerfox COM Service", Ctext)
Endproc

Enddefine

My train of thought

Write this component is "feeling and hair", a while ago with Delphi to write ERP system, although very hard to put a lot of work to the background of SQL Server, in most cases, Delphi end is very simple dataset. However, in some cases involving complex applications, many of the data from SQL Server processing is only semi-finished products, must be processed to the client, this processing is often unusually complex, grouping, sorting, association, Statistics ... The implementation of these functions in Delphi is very cumbersome, in addition to the use of some third-party controls can solve some problems, without exception is a large number of "cycle + conditions", is really very cumbersome, heavy workload.

Based on the above thinking, plus the amount of data processed by SQL Server is not large, I try to write a COM to solve this problem. In addition to the error method, there are only three methods for the entire component: CREATECUROSR (cXml as string,ccursorname as String) converts an externally incoming XML string to a visual FoxPro cursor ; GetCursor (Ccursorname As String) As String is to convert the cursor of visual FoxPro to an XML string, to be passed outward; the key is Execfoxcode (Ccode as String), It can execute multiple sentences with visual FoxPro syntax at one time, so that you can enjoy the vast majority of the functionality provided by Visuial FoxPro on the outside!

Application examples

After compiling this project, we do two simple experiments, feel this super function!

Example one:

Local ox as "POWERFOX.POWERVFP"
Local Cstr,cfoxcode as String
* Establish COM object instance
Ox=createobject ("POWERFOX.POWERVFP")
* Get the result set of XML on client
Use northwind! Customers in 0 SHARED
Cursortoxml ("Customers", "CStr", 3,48,0, "")
Use in Customers
* Pass XML statements to COM and establish cursor
Ox. CREATECUROSR (CSTR, "C1")
* Generate statements that conform to Visual FoxPro syntax
Cfoxcode= "Select Customerid,companyname from C1 WHERE Country like '%s% ' into CURSOR T1"
CFOXCODE=CFOXCODE+CHR (+) + "Select Customerid,companyname,contactname,contacttitle from C1 to CURSOR T2"
* Delivery statements to COM execution
Ox. Execfoxcode (Cfoxcode)
* Let the COM component pass the result set back to the client in the form of XML
Xmltocursor (Ox. GetCursor ("T1"), "RESULT1")
Xmltocursor (Ox. GetCursor ("T2"), "RESULT2")

Case TWO:

Local ox as "POWERFOX.POWERVFP"
Local Cstr1,cstr2,cstr3,cfoxcode as String
* Establish COM object instance
Ox=createobject ("POWERFOX.POWERVFP")
* Get the result set of XML on client
Use northwind! Employees in 0 SHARED
Use northwind! Orders in 0 SHARED
Use northwind! Order_Details in 0 SHARED
Cursortoxml ("Employees", "cStr1", 3,48,0, "")
Cursortoxml ("Orders", "cStr2", 3,48,0, "")
Cursortoxml ("Order_Details", "CStr3", 3,48,0, "")
Use in Employees
Use in Orders
Use in Order_Details
* Pass XML statements to COM and establish cursor
Ox. CREATECUROSR (CSTR1, "Employees")
Ox. CREATECUROSR (CSTR2, "Orders")
Ox. CREATECUROSR (CSTR3, "Order_Details")
* Generate statements that conform to Visual FoxPro syntax
Cfoxcode= "Select A.employeeid,alltrim (LastName) + ' +alltrim (FirstName) as Name,title,sum (unitprice*quantity) * (1- Discount) as Nje "
Cfoxcode=cfoxcode+ "from orders a INNER join Order_Details B on A.orderid=b.orderid INNER join employees c on a.employeeid= C.employeeid GROUP by A.employeeid into cursor T1 nofilter "
CFOXCODE=CFOXCODE+CHR (+) + "Select Employeeid,name,title,max (NJE) as nje from T1 into CURSOR T2"
* Delivery statements to COM execution
Ox. Execfoxcode (Cfoxcode)
* Let the COM component pass the result set back to the client in the form of XML
Xmltocursor (Ox. GetCursor ("T1"), "RESULT1")
Xmltocursor (Ox. GetCursor ("T2"), "RESULT2")

Postscript

Well, about "the meaning of XML in the delivery of COM component data sets" has been discussed in general. I do not know how you feel, I was excited, proud of the mood to do this experiment, write this article. Maybe you didn't agree with the whole experiment--like a psycho. Guide the data set, asking for trouble! But imagine: what would happen if two test programs were not implemented with Visual FoxPro? What a wonderful thing it is! Running Visual FoxPro as a Server is a key thing that Microsoft has been trying to do in recent years, in Visual FoxPro 7, with a large number of enhancements to the technology.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.