项目作者: mdbs99

项目描述 :
Xavier is a small object-oriented XML library for Lazarus and Delphi
高级语言: Pascal
项目地址: git://github.com/mdbs99/xavier.git
创建时间: 2017-09-16T16:42:18Z
项目社区:https://github.com/mdbs99/xavier

开源协议:MIT License

下载


Xavier

License
Hits-of-Code

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.

Table of Contents

Overview

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.

Installing

Clone the repository in some directory in your computer.

Dependencies

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

On Lazarus

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, and mormot_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:

On Delphi

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

Getting Started

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:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3. <foo a1="f1" a2="f2">
  4. <value>foo</value>
  5. </foo>
  6. <bar a1="b1" a2="b2">
  7. <value>bar</value>
  8. </bar>
  9. </root>

Find a Node

If you want to find the value child node of foo node, do this:

  1. var
  2. pack: IXMLPack;
  3. n: IXMLNode;
  4. begin
  5. pack := TXMLPack.Create(
  6. TDataFile.Create('file.xml').Ref.Stream
  7. );
  8. n := pack.Node('/root/foo/value');
  9. ShowMessage(n.Text); // "foo"
  10. end.

In fact, we don’t need variables pack or n. Just call directly:

  1. begin
  2. ShowMessage(
  3. TXMLPack.Create(
  4. TDataFile.Create('file.xml').Ref.Stream
  5. )
  6. .Ref
  7. .Node('/root/foo/value')
  8. .Text
  9. ); // "foo"
  10. end.

Add Node

You can add a node easily doing this:

  1. // add a new node: name="item" value="a"
  2. begin
  3. TXMLPack.Create(TDataFile.Create('file.xml').Ref.Stream)
  4. .Ref
  5. .Node('/root')
  6. .Add('item')
  7. .Text('a')
  8. end;

Childs Count

You can count how many childs a node have doing this:

  1. // How many childs
  2. begin
  3. TXMLPack.Create(TDataFile.Create('file.xml').Ref.Stream)
  4. .Ref
  5. .Node('/root')
  6. .Childs
  7. .Count
  8. end;

Find Attribute

You can find any attribute by name doing this:

  1. // Find by name "id"
  2. begin
  3. TXMLPack.Create(TDataFile.Create('file.xml').Ref.Stream)
  4. .Ref
  5. .Node('/root')
  6. .Attrs
  7. .Item('id')
  8. end;

Add Attribute

Adding an attribute is quite easy too:

  1. // Add an attribute: name="foo" value="bar"
  2. begin
  3. TXMLPack.Create(TDataFile.Create('file.xml').Ref.Stream)
  4. .Ref
  5. .Node('/root')
  6. .Attrs
  7. .Add('foo', 'bar')
  8. end;

License (MIT)

This project is released under MIT license. See LICENSE.