VerySimpleXML – a lightweight Delphi XML reader and writer

Thursday, November 10th, 2011 | Dennis D. Spreen | Delphi 2009, Delphi 2010, Delphi Programming, Delphi XE, Delphi XE2

There are lot of possibilities if you’re in need to parse or write XML files:

Now here comes another one: VerySimpleXML – a lightweight, one-unit XML reader/writer in under 500 600 lines of code. Use it for small well-formed XML files (like configuration files, etc.).

Download verysimplexml-v1.1.zip (7kb)
Download verysimplexml-v1.0.zip (7kb)

What’s new:
v1.1 – 2012/05/01
changed class name to reflect unit naming (class is now called
TXmlVerySimple instead TVerySimpleXml)
– it uses TStreamReader to read line by line instead of reading the
whole file at once (this allows parsing of big xml files with little
additional memory)
– automatically escapes/unescapes not allowed chars (>, <, “, &), thus
removes the most-hated “- escaping introduced in v1.0

Some usage examples:

VerySimpleXML supports just a subset of the XML specification

  • load and save from stream or file
  • nodes, childs and attributes
  • UTF-8 and ANSI encoding
  • compact output by setting Xml.Ident := ”;
  • method chaining
  • “>” and “>” inside text and attribute values when wrapped in quotation marks (XML-spec requires you to transform them into &lt; etc.)now automatically escaped with v1.1

It does NOT support:

  • CDATA, comments, etc…

Example XML-file:

Warning! This is not a standard well-formed XML file (not all attributes are wrapped with quotation marks, < and > are used within attributes and text), it is an example of the (very simple) fault tolerance of VerySimpleXML. Version 1.1 automatically escapes all non allowed chars.

Tags: ,

113 Comments to VerySimpleXML – a lightweight Delphi XML reader and writer

Dennis
March 13, 2014

@angus: please try beta 18, it uses now weak references for the arc nextgen compiler.
you don’t have to use xml.disposeof – just use xmldoc.free as usual.

Angus
March 13, 2014

Dear Dennis,

thanks a lot ,i will check it out.

Angus
March 14, 2014

Dear Dennis,
the Beta 18 works perfect on Android 4.0 now,thanks for your update.

Alessandro Savoiardo
April 2, 2014

Hello Dennis,
if I use this code on Windows:
x := TXmlVerySimple.Create;
x.xml = ‘
città’
The x.ChildNodes.Count is equal 0 but it work on iOS.

I fixed with this code:
in TXmlVerySimple.SetText Line 792
Stream := TStringStream.Create(”, TEncoding.UTF8);

in function TXmlVerySimple.GetText: String; Line 430:
if AnsiSameText(Encoding, ‘utf-8’) then
Stream := TStringStream.Create(”, TEncoding.UTF8)
else
Stream := TStringStream.Create(”, TEncoding.ANSI);

Bye

Alessandro Savoiardo
April 2, 2014

I’m sorry the editor remove xml from code.
The sample is very easy:
NODE DOCUMENT
NODE contain value “città” or accented Characters.
On windows it doesn’t create the nodes with xml property but it work only with Load and Save Stream.

Dennis
April 3, 2014

@Alessandro
Thanks for your bug report and your fix (see beta 21)

Ilia Khubuluri
May 22, 2014

Really great job,
Thanks a lot.
[Delphi developers are still alive]

Ian
June 18, 2014

Dennis, I’m trying to get it to compile in Lazarus (=> Linux).

The only obstacle seems to be the dependency on TStreamReader and TStreamWriter classes, which are painfully slow anyway. Could they be replaced with a targeted (homegrown) replacement?

Targeting Lazarus would enable cross-platform usage of this gem.

Any thoughts?

Grawlix
October 12, 2014

Hello Dennis,

We had some discussion about your excellent work on this component back in February 2014. If I recall correctly, you sent me 1.9 beta, and made reference to 2.0 beta 10, which added escapes to attribute values.

I’ve looked for a more recent version, but do not see it on this site. Have you published it?

Dennis
October 12, 2014

it is finished. most recent version is 2.0 (beta 21) which you’ll find at https://code.google.com/p/verysimplexml/
I’ll update the blog entry the next days and remove the “beta” state – it is considered stable since some months.

[…] Now here comes another one: the updated VerySimpleXML 2.0 – a lightweight, cross-platform, one-unit XML reader/writer for Delphi 2010-XE7 targeting at Win32, Win64, iOS, MacOSX and Android. Use it for well-formed XML files (like configuration files, interprocess communication protocols, etc.). […]

Dennis
October 13, 2014

See above comment for the new 2.0 version.

About Dennis D. Spreen

I'm an avid programmer working on a variety of platforms in a variety of languages with a wide technical interest.

Search

QR Code

Categories