Last year I spent a lot of time trying to use DELPHI Based on XML Of WEB application development . The original idea was beautiful , But it turned out to be very crude . Part of the reason is XML To Object The implementation of data binding between is too cumbersome ( Another part of it is because of XSLT Not familiar with , It took a lot of time to learn it ).

I used to use DELPHI Provided XML Data binding To do the , The basic approach is : Use tools first ( Such as XMLSPY) Make a good one XML Schema(XSD), And then use XML Data binding Generate DELPHI The interface and class of . Of course , Once generated, it's convenient , In the program, I just need to operate this interface , Each of them Field Will be turned into attributes , And the types are just like me XSD Defined in the . But the problem is that in the development process , There will always be some changes , under these circumstances , I had to drive it at the same time XMLSPY modify XSD, And then use it again XML Data binding Of Wizard Run it over , Very troublesome .

So when I think about objectification of datasets , Immediately think that you can also use RTTI To achieve Object Of XML Persistence -- Actually DELPHI6 At the beginning SOAP Implementation is to use RTTI To achieve Object To SOAP data ( Namely XML) The transformation of . Obviously, I'm very much behind the scenes , When I was there 《 Powerful DELPHI RTTI-- Also talk about the need to understand a variety of development languages 》 When it comes to my plan in the article , friend Lex CHow In reply, I said that he had done this work about a year ago , I immediately asked him to come to his source code .lexlib He wrote a library with many functions , It looks a little bit like the structure .net The basic class library of ( Not that big, of course ^O^),Object Of XML Persistence is just a small part of it . Because I only need this part , There's no need to use the whole library so much trouble , So I refer to lexlib And combined with me in 《 use DELPHI Of RTTI Realize the simple objectification of data set 》 What has been implemented in , Made a simple implementation :

TMXMLPersistent = class(TObject)
public
class Procedure LoadObjFromXML( aNode : IXMLNode; aObj : TPersistent );
class Procedure SaveObjToXML( aNode : IXMLNode; aObj : TPersistent );
end; const
DefaultFilter : TTypeKinds = [tkInteger, tkChar, tkEnumeration,
tkFloat, tkString, tkSet, tkWChar, tkLString, tkWString, tkInt64]; { TMXMLPersistent } class procedure TMXMLPersistent.LoadObjFromXML(aNode: IXMLNode;
aObj: TPersistent);
Var
i : Integer;
pList : TMPropList;
pInfo : PPropInfo;
tmpObj: TObject;
begin
If ( aObj Is TMDataSetProxy ) Then
( aObj As TMDataSetProxy ).LoadFromXML( aNode )
Else
Begin
pList := TMPropList.Create( aObj );
Try
For i := 0 To pList.PropCount - 1 Do
Begin
pInfo := pList.Props[i];
If ( pInfo^.PropType^.Kind = tkClass ) Then
Begin
tmpObj := TObject( Integer( GetPropValue( aObj, pInfo^.Name ) ) );
If ( Assigned( tmpObj ) AND ( tmpObj Is TPersistent ) ) Then
LoadObjFromXML( aNode.ChildNodes[WideString(pInfo^.Name)],
tmpObj As TPersistent );
End
Else If ( pInfo^.PropType^.Kind In DefaultFilter ) Then
SetPropValue( aObj, pInfo^.Name,
String( aNode.ChildNodes[WideString( pInfo^.Name )].Text ) );
End;
Finally
pList.Free;
End;
End;
end; class procedure TMXMLPersistent.SaveObjToXML(aNode: IXMLNode;
aObj: TPersistent);
Var
i : Integer;
pList : TMPropList;
pInfo : PPropInfo;
tmpObj: TObject;
begin
If ( aObj Is TMDataSetProxy ) Then
( aObj As TMDataSetProxy ).SaveToXML( aNode )
Else
Begin
pList := TMPropList.Create( aObj );
Try
For i := 0 To pList.PropCount - 1 Do
Begin
pInfo := pList.Props[i];
If ( pInfo^.PropType^.Kind = tkClass ) Then
Begin
tmpObj := TObject( Integer( GetPropValue( aObj, pInfo^.Name ) ) );
If ( Assigned( tmpObj ) AND ( tmpObj Is TPersistent ) ) Then
SaveObjToXML( aNode.AddChild( WideString( pInfo^.Name ) ),
tmpObj As TPersistent );
End
Else If ( pInfo^.PropType^.Kind In DefaultFilter ) Then
aNode.AddChild( WideString( pInfo^.Name ) ).Text :=
GetPropValue( aObj, pInfo^.Name );
End;
Finally
pList.Free;
End;
End;
end;

This implementation should be said to be very simple . There are mainly three parts (Load and Save It's similar in structure ):

One is right TMDataSetProxy Special treatment , Delegate to the class itself to handle its implementation , Because it's different from ordinary classes , Need to pass through ForEach Traverse all records , That's actually how to implement data sets at the same time XML Persistence .

The second is right Class Do recursive processing , Of course, only from TPersistent Derived class.

The third is general Field Simply turn to String preservation , It draws lessons from lexlib Of Filter, Only those that can be converted into String Data type of , Filter out the types that may cause conversion errors .

Used in the above code TMPropList see 《 use DELPHI Of RTTI Realize the simple objectification of data set 》 In the implementation of .

The following is to use TMDataSetProxy Implementation of the data set of XML Persistence . No need to go through TClientDataSet The trouble with it , And it uses Node How to record fields ,.net It's the same way , And TClientDataSet What's used is Attribute The way fields are recorded is different . Although it's generated in this way XML The file will be a little bigger , But the benefits are obvious , Especially when I'm going to use it Web In application , use Node It's recorded in this way XML, In use XSLT It will be much more convenient .

procedure TMDataSetProxy.LoadFromXML(aNode: IXMLNode);
Var
i, j : Integer;
pInfo : PPropInfo;
pRow : IXMLNode;
begin
For j := 0 To aNode.ChildNodes.Count - 1 Do
Begin
FDataSet.Append;
pRow := aNode.ChildNodes[j];
For i := 0 To FPropList.PropCount - 1 Do
Begin
pInfo := FPropList.Props[i];
If ( pInfo^.PropType^.Kind In DefaultFilter ) Then
SetVariant( i,
String( pRow.ChildNodes[WideString( pInfo^.Name )].Text ) );
End;
EndEdit;
End;
FDataSet.First;
end; procedure TMDataSetProxy.SaveToXML(aNode: IXMLNode);
Var
i : Integer;
pInfo : PPropInfo;
pRow : IXMLNode;
begin
While ForEach Do
Begin
pRow := aNode.AddChild( 'Row' );
For i := 0 To FPropList.PropCount - 1 Do
Begin
pInfo := FPropList.Props[i];
If ( pInfo^.PropType^.Kind In DefaultFilter ) Then
pRow.AddChild( WideString( pInfo^.Name ) ).Text
:= GetVariant( i );
End;
End;
end;

Here's a simple one DEMO, It includes a study of the data set XML Persistence . Be careful Load When Employee Members are connected to ADODataSet2, It connects to a table that contains these fields , Each field type is related to Employee The table is the same , But the content is empty , And removed EmployeeID Of Identity.Load After completion ,Employee The contents of these fields in the table will be copied to this table .

TDemoCompany = class( TPersistent )
private
FEmployee : TDSPEmployee;
FCompany : String;
FCode : Integer;
published
property Employee : TDSPEmployee Read FEmployee Write FEmployee;
property Company : String Read FCompany Write FCompany;
Property Code : Integer Read FCode Write FCode;
End; procedure TForm1.SaveClick(Sender: TObject);
Var
demo : TDemoCompany;
begin
demo := TDemoCompany.Create;
demo.Employee := TDSPEmployee.Create( ADODataSet1 );
demo.Company := 'Demo company';
demo.Code := 987654;
Try
XMLDocument1.Active := true;
TMXMLPersistent.SaveObjToXML( XMLDocument1.AddChild( 'Demo' ), demo );
XMLDocument1.SaveToFile( 'temp.xml' );
XMLDocument1.Active := false;
Finally
demo.Employee.Free;
demo.Employee := Nil;
demo.Free;
End;
end; procedure TForm1.LoadClick(Sender: TObject);
Var
demo : TDemoCompany;
begin
demo := TDemoCompany.Create;
demo.Employee := TDSPEmployee.Create( ADODataSet2 );
Try
XMLDocument1.Active := true;
XMLDocument1.LoadFromFile( 'temp.xml' );
TMXMLPersistent.LoadObjFromXML( XMLDocument1.ChildNodes.Last, demo );
XMLDocument1.Active := false;
Edit1.Text := demo.Company;
Edit2.Text := IntToStr( demo.Code );
While ( demo.Employee.ForEach ) Do
With ListView1.Items.Add Do
Begin
Caption := IntToStr( demo.Employee.EmployeeID );
SubItems.Add( demo.Employee.FirstName );
SubItems.Add( demo.Employee.LastName );
SubItems.Add( FormatDateTime( 'yyyy-mm-dd', demo.Employee.BirthDate ) );
End;
Finally
demo.Employee.Free;
demo.Employee := Nil;
demo.Free;
End;
end;

Finally, I can say goodbye to the trouble XML Data binding 了 , And I don't have to write in the future XSD 了 -- Although there are useful tools , But it's good to save something .

http://blog.csdn.net/diligentcatrich/article/details/6934109

use DELPHI Of RTTI Of the implementation object XML More about persistence

  1. use DELPHI Of RTTI Realize the simple objectification of data set

    stay < Powerful DELPHI RTTI-- Also talk about the need to understand a variety of development languages > In the article , I said I use DELPHI Of RTTI Realize the simple objectification of data set . This article will introduce my implementation method in detail .      First, from a simple ...

  2. Delphi Of RTTI On the mechanism 3( Super long , Very good )

    from :http://blog.sina.com.cn/s/blog_53d1e9210100uke4.html Catalog ========================================== ...

  3. adopt JAXB complete Java Object and the XML Conversion between

    Java Object conversion XML The process is called marshal. XML The switch to Java The process of object is called unmarshal. One .Java Object to XML It is omitted here getter and setter Method By annotation @XMLRootEl ...

  4. java Object and the XML Reciprocal transformation

    cause Recently, I did a webservice Related tasks , One of the things I'm most interested in is webservice Accept to XML How can the corresponding stream be transformed into java object , and java How to generate the corresponding object XML Of . Purpose ...

  5. XmlSerializer Object's Xml Serialization and deserialization

    http://www.cnblogs.com/yukaizhao/archive/2011/07/22/xml-serialization.html This essay corresponds to .Net The namespace is System.Xm ...

  6. The serialized object is xml character string

    /// <summary>    /// The serialized object is xml character string     /// </summary>    /// <param name="obj" ...

  7. Java Object representation 2:XStream To implement the XML turn

    The last article talked about using Java Representing an object in a native serialization way . Summarize the advantages and disadvantages of this object representation : 1. Pure Java It works well in this environment , Because it is Java Self contained , There's no need for a third party Jar Package support ...

  8. JAVA Objects and XML file 、 It turns out there's another one between them

    Recently, we encountered a problem in project development , The access interface no longer passes through url The way the address is requested , It is adopt socket send out xml Message format to the designated server for unified authentication of information .. So assemble xml Format of the message string and the parsing server returned xm ...

  9. Get along well with Java Objects and XML transformation

    It's been appearing in projects recently Java Objects and XML Mutual conversion between , At first, because the project was huge , I'm a temporary dispatcher again , As a result, while writing the code according to the requirements of the project leader , Always in this cloud , It's only recently that I've begun to figure out the specific reasons for the use of the project ...

Random recommendation

  1. word-wrap: break-word; and word-break: break-all; The difference between

    Check out the links below .( Reprinted from Zhang Xinxu great God space ) http://www.zhangxinxu.com/wordpress/2015/11/diff-word-break-break-all-word-wra ...

  2. Java Collection Series 01 And General framework

      Java A set is java The kit provided , Contains common data structures : aggregate . Linked list . queue . Stack . Array . Mapping, etc. .Java The location of the assembly kit is java.util.*Java The set can be divided into 4 Parts of :List list .Set ...

  3. jquery api call

    The built-in components and some plug-ins of this framework can be accessed through jquery The selector goes on API call , Support chain operation , The following example . <script type="text/javascript"> $(&q ...

  4. spoj 237

    Good question   ha-ha #include <cstdio> #include <algorithm> #define S(n) scanf("%d",&n ...

  5. zookeeper Implement distributed locks

    Distributed lock services may not be used much in your projects , Because everyone put exclusivity on the database level to block . When a lot of row locks . Table locks . When transactions fill the database . commonly web The bottleneck of many applications lies in the database , Here is a way to reduce the burden of database lock ...

  6. Note | Javascript Authoritative guide [ The sixth edition ] The first 1 Chapter :Javascript summary

       JavaScript It's a high-end . Dynamic . Weak type programming language , Very suitable for object-oriented and functional programming style .JavaScript The grammar of comes from Java, Its first-order function (first-class function) Come on ...

  7. ops

    consists several key projects separately stand-alone connected entities massive scalability massive ...

  8. After logging into the database ,use db It's a slow question

    mysql> use dbl Reading table information for completion of table and column names You can turn of ...

  9. Springboot Integrate Kfka

    1. First, in the pom File add dependency The managed version is 1.1.7.RELEASE The artifact is managed in org.springframework.b ...

  10. redis Operation and maintenance manual

    redis cli command - milkty - Blog Garden https://www.cnblogs.com/kongzhongqijing/p/6867960.html Redis Multiple databases - EasonJ ...