8/27/2019 Delphi Serialize Xml
Enswafford 13-Jul-Jul-19 10:50I have used this code in an application (with only modifying the element names and using variables for the data), but it overwrites each time I run the code. I want to be able to append additional items to the XML file each time I run the code.For example, the code will create the following XML file:4RohiniDelhiBut I want to be able add a second entry so the file looks like this:4RohiniDelhi1234AnyMy TownWhat do I need to do to append a new AddressDetails block?EDIT: NOTE: the data that already exists in the file will only exist in the file, not in memory. And I am trying to avoid recreating the entire file each time as there could be hundreds if not thousands of entries in the XML file. Nitancy 19-Mar-15 9:3719-Mar-15 9:37I think I have run across some errors in the code in the article by “Mayank Gupta 688” entitled 'XML Serialization and Deserialization: Part-1'. Bob Delaney 6-Jan-15 7:176-Jan-15 7:17Nice article, and my compliments. I am looking forward to trying what you have laid out in this article. May I suggest doing similar treatment to JSON?
I have been grappling with it, and with XML, for some time now, trying not very successfully to make XML and JSON work on ASP.Net Webforms pages in C# and, depending on the circumstances on the site, JavaScript. This is somewhat outside the scope of your fine article, and I add it as a suggestion for perhaps a future article.
This means it must be well-formed, include a prolog and DTD, and all elements must be closed. The first element in the document is the element. This includes a mandatory version attribute.
The next element is the element. This is the main container for all RSS data. The element is the title, either of the entire site (if it's at the top) or of the current item (if it's within an ). The element indicates the URL of the Web page that corresponds to the RSS feed, or if it's within an, the URL to that item. The element describes the RSS feed or the item. The element is the meat of the feed.
Delphi Serialize Xml Download
These are all the headlines , URL and description that will be in your feed. VarStartItemNode: IXMLNode;ANode: IXMLNode;STitle, sDesc, sLink: WideString;begin.//points to local XML file in 'original' codeXMLDoc.FileName:= ':= XMLDoc.DocumentElement.ChildNodes.First.ChildNodes.FindNode('item');ANode:= StartItemNode;repeatSTitle:= ANode.ChildNodes'title'.Text;sLink:= ANode.ChildNodes'link'.Text;sDesc:= ANode.ChildNodes'description'.Text;//add to list viewwith LV.Items.Add do beginCaption:= STitle;SubItems.Add(sLink);SubItems.Add(sDesc)end;ANode:= ANode.NextSibling;until ANode = nil.
Components(8)I use XML for all my application as means of configuration. It is:. flexible.
future feature proof. easy to read with any text reader. very easy to extend in application. No class modifications neededI have an XML library that makes it extremely easy to read or modify configuration, without even having to watch for missing values.
Now you can also map the XML to a class inside application for faster access if speed is the issue, or certain values are read constantly.I find other configuration methods far less optional:. Ini file: no in depth structure, far less flexible.
registry: just keep away from that. The built-in RTTI based system for serializing published properties is vulnerable to changes in the components. Going forwards is manageable as long as old properties are kept in new objects. You leave the property interface as is, but can toss away the contents if you like. Going backwards is worse - as a newer version saved property can't be opened in older version load, and that will be a problem.There are components / libs that can add serialization in XML format and this may help a bit as you can choose to skip content you don't know.You still need to be mindful about how you design your published content and should probably find a way to 'ignore but propagate' content that your current version don't understand. This will allow you to open and change a file in a newer format while attempting to keep newer attributes, instead of stripping them.
![]()
What is the best way to serialize Delphi application configuration?This is my proposed solution.I have a base class TConfiguration = classprotectedtypeTCustomSaveMethod = function ( Self: TObject; P: Pointer ): String;TCustomLoadMethod = procedure ( Self: TObject; const Str: String );publicprocedure Save ( const FileName: String ); procedure Load ( const FileName: String ); end;The Load methods look like this (Save method accordingly): procedure TConfiguration. Load ( const FileName: String ); constPropNotFound = 'PROPNOTFOUND'; varIniFile: TIniFile;Count: Integer;List: PPropList;TypeName, PropName, InputString, MethodName: String;LoadMethod: TCustomLoadMethod; beginIniFile:= TIniFile. Create ( FileName );tryCount:= GetPropList ( Self.
ClassInfo, tkProperties, nil );GetMem ( List, Count. SizeOf ( PPropInfo ));tryGetPropList ( Self. ClassInfo, tkProperties, List ); for I:= 0 to Count - 1 do beginTypeName:= String ( List I ^. Name );PropName:= String ( List I ^.
Serialize Xml File
Name );InputString:= IniFile. ReadString ( 'Options', PropName, PropNotFound ); if ( InputString = PropNotFound ) thenContinue;MethodName:= 'Load' + TypeName;LoadMethod:= Self.
MethodAddress ( MethodName ); if not Assigned ( LoadMethod ) thenraise EConfigLoadError. Create ( 'No load method for custom type ' + TypeName );LoadMethod ( Self, InputString ); end;finallyFreeMem ( List, Count.
SizeOf ( PPropInfo )); end;finallyFreeAndNil ( IniFile ); end;The base class could provide load and save methods for the delphi default types. I can then create a configuration for my application like this: TMyConfiguration = class ( TConfiguration ).publishedfunction SaveTObject ( P: Pointer ): String; procedure LoadTObject ( const Str: String );publishedproperty BoolOption: Boolean read FBoolOption write FBoolOption;property ObjOption: TObject read FObjOption write FObjOption; end;Example of a custom save method: function TMyConfiguration. SaveTObject ( P: Pointer ): String; varObj: TObject; beginObj:= TObject ( P );Result:= Obj. ClassName; // does not make sense; only example; end.
Delphi DLL Examples(Delphi DLL) Serialize / Deserialize Hashtable to/from XMLDemonstrates how to seralize / deserialize a Hashtable to/from XML.Note: This example requires Chilkat v9.5.0.64 or later. Chilkat for Delphi Downloads. The examples here use the non-ActiveX DLL.usesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringTable, StringBuilder, Hashtable.procedure TForm1.Button1Click(Sender: TObject);varhashTab:;sb:;hashTab2:;sTable:;i: Integer;numKeys: Integer;key: PWideChar;begin // Note: This example requires Chilkat v9.5.0.64 or later. // Add some entries to a hashtable.hashTab:= CkHashtableCreate;CkHashtableAddStr(hashTab, 'aaa', '111');CkHashtableAddStr(hashTab, 'bbb', '222');CkHashtableAddStr(hashTab, 'ccc', '333');// Serialize to XMLsb:= CkStringBuilderCreate;CkHashtableToXmlSb(hashTab,sb);Memo1.Lines.Add( CkStringBuildergetAsString(sb));Memo1.Lines.Add( '-');// The output is as follows.
Each hash table entry // is contained in an 'e' node. The entry's key // is in the 'k' node, and the value in the 'v' node. // // // aaa111 // bbb222 // ccc333 // // // Now load (deserialize) into a new hash table.hashTab2:= CkHashtableCreate;CkHashtableAddFromXmlSb(hashTab2,sb);// Get the hash table keys, and lookup each (to show // that the hash table was correctly deserialized). // The GetKeys method can return the keys in any order.sTable:= CkStringTableCreate;CkHashtableGetKeys(hashTab2,sTable);i:= 0;numKeys:= CkStringTablegetCount(sTable);while i.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |