Simple XML based storage – Part I.
For quite some time now, I was being bothered by the fact that every time I use XML as a data transmitting media, I write a lot of code just to read and write from / to XML. I use XML to maintain config files for quite a few project. Furthermore I use it in a few projects as means of transferring data. For instance here is an event handler that is used in a rendering engine when the engine requests some external data to process.
procedure OnRenderEngineCustomTag(const TagName: TUTF8String; const TagParams: TStrings; var TagData: ISimpleStorage);
So I looked for means that would allow me to transfer any kind of data over and do that with as little code as possible. Needed qualities were (not in any particular order):
- flexibility,
- ease of use,
- structured type of storage.
As a cherry on the pie, it would be nice if I could use this to parse my config files and to use it as a method of serialization in SOAP data transfers. This way I could read / write SOAP data with ease.
I realized that even if it is not the fastest technology out there, XML would be perfect for this.
- It is Flexible and can hold almost any kind of data. It even can hold attributes to describe the data.
- It is well accepted, so the tools are numerous and almost everyone can read it.
- It can be manipulated by hand if need arises.
- It is well structured by nature.
I already use XML a lot in my day to day work. OmniXML is a wonderful Delphi XML library that is flexible and powerful. It also already has a lot of supporting code and tools build around it. So I decided to build on top of OmniXML. Actually it was a no brainier
.
The whole idea of the storage I had, was build around nodes and values. They are both elements. The storage has a root element and any number of elements further down the tree structure. An element is a node if it has at least one child. And all the elements without any children are values. Both can have attributes that describe them even better, but only values can hold actual data. So a typical storage would look like this. You can map this logic to lets say some of the tree data structures.
<Books> <English> <Science> <Mathematics Difficulty="High">5</Mathematics> <Physics Difficulty="Low">0</Physics> <Computers Difficulty="Medium">8</Computers> <Engineering Difficulty="Low">4</Engineering> <Electronics Difficulty="High">8</Electronics> </Science> <Musical> <Classic>2</Classic> <Etno>2</Etno> <Pop>8</Pop> </Musical> </English> </Books>
Here we have a book storage, which holds two type of books. Science books and Musical books. These are further divided by the category to which they belong. Here we can see that “Science” and “Musical” elements are typical nodes that have values. “Mathemathics” and “Physics” are values under the “Science” node and “Etno” is a value under the “Musical” node. They all hold actual data. We can see that we have 5 “Mathemathical” books and 0 “Physics” books. And all “Science” books have and attribute “Difficulty” that further divides them into groups.
Now you probably already have the idea how the things are meant to work. So I am just going to demonstrate a little sample of how easy it is to write or read using the SimpleStorage.
SourceStorage := StorageFromFile('BookStorage.xml'); SourceStorage.Get('English/Science/Mathematics').AsInteger; SourceStorage.Get('English/Science/Computers').AsString; SourceStorage.Ensure('English/Science/Arheology').AsInteger := 3; SourceStorage.Ensure('English/Science/Geology').AsInteger := 15;
In the first two lines we read the “Mathematics” and “Computers” values as integer and as string. It is really easy to access any node in the tree and also read it in a needed value type. The second two lines demonstrate how easy it is to write two new values (“Arheology” and “Geology”) to the storage. As you can see you can use simplified XPath to access the nodes which gives you great flexibility.
For now this will be all. Next time I will go further and show advanced methods of data manipulation.
The code is considered to be stable. But there may still be some bugs in it. To download the code with the demo get it here – SimpleStorage
From Zero To One » Blog Archive » What is new in SimpleStorage wrote,
[...] Simple XML based storage – Part I. [...]
Link | February 7th, 2010 at 2:37 pm
From Zero To One » Blog Archive » Build your XML the LinqToXML style in native code – Part I. wrote,
[...] http://www.cromis.net/blog/2008/07/simple-xml-based-storage/ [...]
Link | March 9th, 2010 at 10:12 am