There are lot of possibilities if you’re in need to parse or write XML files:
- use TXMLDocument (the MS XML wrapper)
- or use a xml component like OmniXML, NativeXml, ADOM, SAX, libxml2, DIXml, fcl-XML, Fast XMLParser, SimpleXML, OXml, or VerySimpleXML 1.0
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.).
Download current version at Google Code
VerySimpleXML 2.0 is still open sourced and open for anyone to participate in the further development.
What’s new in version 2.0?
- Support for these nodes types: ntElement (default node type), ntComment (comment nodes), ntDocType (Document Type Definition, but not parsed/evaluated), ntText (text only nodes), ntCData (CDATA sections), ntProcessingInstr (processing instructions)
- Windows 32 bit, Windows 64 bit, Mac OSX, iOS and Android compatible
- Support for headless xml files
- Reduced memory consumption
- Allows now processing of large xml files without line breaks
- Node content is now read exactly as found (including line breaks)
- Case sensitive/insensitve node and attribute name handling (use doCaseInsensitive option)
- Write UTF-8 files with or without BOM (doWriteBOM option)
- Support for attributes without values (single attribute type)
- Support for the Delphi “nextgen compiler” and weak references for ARC
- Added a lot of TXMLDocument compatible methods
- Support for parsing the ntProcessingInstr nodes (processing instructions)
- Support for Windows, Unix and even custom line breaks
- Improved fluent interfaces
What’s the difference between version 1.x and version 2.0?
a) Root and Header nodes
There is no XmlDoc.Root
element any more (it’s still there but hidden in the private declarations part) because it’s renamed to XmlDoc.DocumentElement
to be more TXMLDocument compatible and it’s not created by default now.
Old version 1.0
Xml := TXmlVerySimple.Create; Xml.Header.Attribute['encoding'] := 'utf-8'; Xml.Root.NodeName := 'books'; Node := Xml.Root.AddChild('book'); Node.Attribute['id'] := 'bk103'; Node.Text := 'A book'; Xml.SaveToFile('test.xml'); Xml.Free;
New version 2.0
Xml := TXmlVerySimple.Create; // Create a new XmlDocument with default encoding "utf-8" //Xml.Encoding := 'utf-8'; // A more convinient way to change the encoding //Xml.Header.Attribute['encoding'] := 'utf-8'; // is still valid // Add a new child node, the first child node is the DocumentElement Xml.AddChild('books'); // Add a new book node to the document root Node := Xml.DocumentElement.AddChild('book'); Node.Attributes['id'] := 'bk103'; Node.Text := 'A book'; Xml.SaveToFile('test.xml'); Xml.Free;
You may have noticed that Node.Attribute['someattrib']
is now renamed to Node.Attributes['someattrib']
as well.
The above example still works by using fluent interfaces:
Xml := TXmlVerySimple.Create; Xml.AddChild('books').AddChild('book').SetAttribute('id', 'bk103').Text := 'A book'; Xml.SaveToFile('test.xml'); Xml.Free;
or even written as a one liner : -)
TXmlVerySimple.Create.AddChild('books').AddChild('book').SetAttribute('id', 'bk103'). SetText('A book').Document.SaveToFile('test.xml').Free;
The next gen compiler hints “H2593: code has no effect”, so you may omit the last .Free
…
b) Last line break
As VerySimpleXML 2.0 operates now on read and write buffers instead of string lines, it omits the last line break of a xml file when written.
How to upgrade to version 2.0?
- Replace
VerySimpleXml.pas
with the newVerySimpleXml.pas
- Replace
Xml.Root.NodeName
assignments withXml.AddChild
- Replace
Node.Attribute[]
withNode.Attributes[]
Come on – yet another XML parser?
About 3 years ago I was in a need for parsing some xml files, so I’ve started using TXMLDocument. But because it was a little bit slow, I’ve decided to write my own very simple xml parser (hence the name) because I couldn’t find an easy to use, “unbloated” xml component. VerySimpleXML was done in about two afternoons – I’m good in writing parsers ; -) and is still used in a lot of projects. At the end of last year I’ve upgraded to Delphi XE5 and startet porting the xml parser to the next gen compiler. And added a lot more features – mainly support for different node types like comments and cdata. VerySimpleXML is not “feature-complete”, nor is it the fastest delphi xml parser found (but still faster than TXmlDocument) – it is just a small and simple parser for small and simple xml files. If you need something more versatile, I highly recommend OXml – The next generation XML library for Pascal written by Ondřej Pokorný (the same author who updated and bug fixed the great OmniXML component) which even runs with Delphi 6 and FPC, Lazarus and C++Builder.
Found a bug or need help?
- Use the comment system below
- Use the google code issues list for reporting bugs or feature requests
- See Wiki:Help for documentation
- Take a look at the included examples
- Write me at dennis@spreendigital.de