Mega Code Archive

 
Categories / Delphi / ADO Database
 

How to use XML file with DATASET

Title: How to use XML file with DATASET? Question: How to use XML file with DATASET? Answer: Hi, In this article I prefer to say some thing about XML. When you use a Dataset if use ADO or BDE your program have to install Borland Database engines or something else same as SQL server or Oracle. But in this article I will help you to write some programs that don't need to install a database engines for use. The main point is we will use XML data format instead of database engines. When you want to use XML files one way is use TCLIentdataset. Now with me go step by step to xml universe. First you have to create an xml document. The simplest way is this: And save this to a file named test.xml Every xml file have tags same as and tags start with and end with .in the between of a pair tag data will come. Ok, now you have test.xml with content of above. 2.start XMLmaper.exe from start/all programs/Borland delphi7/xmlmapper Select file open and open your xml file. On the document view try right click and use "select all" item. After that again use right click and select create data pack form xml or CTRL+D. Uses create and test transformation and after it on field view use right click and select clear. A dialog box appears that want a file name save it to test.xtr the xtr file content is your database style." no data" and a transform file for use. Now start Delphi. Use these components: DataSource1 ClientDataSet1 XMLTransformProvider1 DBGrid1 DBNavigator1 Button2 You have to use a button for save. And use this code for you're save button var str:tfilestream; s:string; i:integer; begin str:=tfilestream.Create(XMLTransformProvider1.XMLDataFile,fmcreate); try clientdataset1.First; s:=''; str.Write(s[1],length(s)); while not clientdataset1.Eof do begin s:=''; for i:=0 to clientdataset1.FieldCount-1 do s:=s+makexmlstr(clientdataset1.Fields[i].FieldName,clientdataset1.Fields[i].AsString); s:=makexmlstr('cachedata',s); str.Write(s[1],length(s)); clientdataset1.Next; end; s:=''; str.Write(s[1],length(s)); finally str.Free; end; end; in XMLTransformProvider1.xmldatafile:='test.xml'; And your transform reader and writer use test.xtr that created with xml mapper. Run program and enter some data in dbgrid After it save your data Now you can com back to xml mapper in the sheet of node properties and change your field's type and size and create xtr again. Have a good day with XML