Xavier is a small XML library, object-oriented, and cross-platform that simplifies the work with XML files and streams using XPath.
ATTENTION: We're still in a very early alpha version, the API may and will change frequently. Please, use it at your own risk, until we release version 1.0.
This API is being written in Free Pascal and Lazarus. However, it is also compatible with Delphi.
Most XML libraries are very complex. Each class has so many methods that could be hard to use and understand them. These implementations are very procedural too.
The main goal is to replace common procedural code, which could have so many conditionals and variables, to a declarative and object-oriented code to work more easily with XML.
The code follows a restrict rules about naming and style, as prefixes and suffixes, to help programmers to find the correct class or method to do the job quickly.
Clone the repository in some directory in your computer.
Internally, this project uses the built-in XML library for each compiler.
Besides that, we are using other libraries:
- James — is a collection of classes and interfaces for truly object-oriented projects.
- mORMot — client-server ORM SOA MVC framework for Delphi 6 up to latest Delphi and FPC
It has been tested using these versions:
- FPC 3.1.1 revision 40491
- Lazarus 2.1.0 revision 59757
To install on Lazarus:
- Make sure that you have
JamesLib.lpk
, andmormot_base.lpk
available - see dependencies - Open the package in
/pkg/XavierLib.lpk
- Compile it
- That's all.
The IDE will be aware about XavierLib Package to use in any project.
Considering <xavier>
as the path where you have saved the sources, your project must include these paths:
Considering <xavier>
as the path where you have saved the sources, you must include these paths in your project:
- Search Path
<xavier>\src;<xavier>\src\delphi
You can find some examples to use Xavier in its own source. Just take a look into Xavier*Tests
units.
Additionally, below you can find the basics to start.
Take this XML for all examples below:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<foo a1="f1" a2="f2">
<value>foo</value>
</foo>
<bar a1="b1" a2="b2">
<value>bar</value>
</bar>
</root>
If you want to find the value
child node of foo
node, do this:
var
pack: IXMLPack;
n: IXMLNode;
begin
pack := TXMLPack.Create(
TDataFile.Create('file.xml').Ref.Stream
);
n := pack.Node('/root/foo/value');
ShowMessage(n.Text); // "foo"
end.
In fact, we don't need variables pack
or n
. Just call directly:
begin
ShowMessage(
TXMLPack.Create(
TDataFile.Create('file.xml').Ref.Stream
)
.Ref
.Node('/root/foo/value')
.Text
); // "foo"
end.
You can add a node easily doing this:
// add a new node: name="item" value="a"
begin
TXMLPack.Create(TDataFile.Create('file.xml').Ref.Stream)
.Ref
.Node('/root')
.Add('item')
.Text('a')
end;
You can count how many childs a node have doing this:
// How many childs
begin
TXMLPack.Create(TDataFile.Create('file.xml').Ref.Stream)
.Ref
.Node('/root')
.Childs
.Count
end;
You can find any attribute by name doing this:
// Find by name "id"
begin
TXMLPack.Create(TDataFile.Create('file.xml').Ref.Stream)
.Ref
.Node('/root')
.Attrs
.Item('id')
end;
Adding an attribute is quite easy too:
// Add an attribute: name="foo" value="bar"
begin
TXMLPack.Create(TDataFile.Create('file.xml').Ref.Stream)
.Ref
.Node('/root')
.Attrs
.Add('foo', 'bar')
end;
This project is released under MIT license. See LICENSE.