MicroXML (updated 07-FEB-2009)

MicroXML utilizes Stefan Heymann's XML parser to generate an XML DOM using Delphi classes.

Send comments/suggestions/jokes/complaints/ramblings to rmorales2005 at gmail.com.


MicroXML.zip (does NOT include the parser)

Updated on 07-FEB-2009 to fix a simple bug with saving element attributes - thanks to Steffen Thorkildsen!

Download Stefan Heymann's XML parser and be sure to include both LibXmlParser.pas and MicroXml.pas in your project.


Tested with Turbo Delphi 2006, and FreePascal 2.2.0 (using -Mdelphi).


MicroXML is very easy to use, as there's only three classes to learn:


This class is used to describe attributes found in an XML element.


Property Type Description
Name string The attribute's name.
Value string The attribute's value.


This is the most important class in MicroXML. TMicroXmlElement represents all elements in the XML document, starting from the root element.


Property Type Description
Parent TMicroXmlElement

Indicates the parent of this XML element.

For the root node, Parent is nil.

Name string The XML element's tag name.
Text string The text contents, if any, of the XML element.
Attributes Array Provides access to the XML element's attributes.
AttributeCount Integer Returns the number of attributes.
Children Array Provides access to children of this XML element.
ChildCount Integer Returns the number of children.


constructor Create(AParent: TMicroXmlElement; const AName: string);

Default constructor. Do not directly construct TMicroXmlElement objects yourself.

procedure SetAttribute(const Name, Value: string);

Assign the string in Value to the attribute Name. If the attribute does not exist, it will be created.

function GetAttribute(const Name: string): string;

Retrieve the value of an attribute by name. If the attribute does not exist, an empty string is returned.

function AddChild(const Name: string): TMicroXmlElement;

Add a new child element to this XML element. A TMicroXmlElement object is returned for your use.

procedure AddChildStrings(const Name: string; const Strings: TStrings);

Each string in Strings will be added as a child element, using the specified element name.

procedure DeleteChild(Index: Integer);

Remove a child element.

function IndexOf(const Element: string): Integer;

Locate a child element by name. A zero-based index is returned: 0 for the first element, 1 for the second, etc. If the element is not found, -1 is returned.

function GetChildText(const Element: string): string;

A convenience function to retrieve the text of a child XML element. If the element does not exist, an empty string is returned.


This class loads XML documents and generates a DOM.


Property Type Description
RootElement TMicroXmlElement

The XML document's root element.

This will be nil until an XML document is loaded via LoadFromFile, or started via NewDocument.


procedure NewDocument(const RootElemName: string);

Sets up the TMicroXmlDocument object to start a new XML DOM. RootElemName is the name of the document's root element.

procedure LoadFromFile(const FileName: string);

Creates an XML DOM from the specified file. WARNING: TMicroXmlDocument only supports one root element. MicroXML's behavior is undefined for documents that have multiple root elements.

procedure SaveToFile(const FileName: string);

Save the XML DOM to the specified file.

Example #1 - Reading an XML file

This program will load a file called "test.xml" and print out its contents.

program xmltest;

  SysUtils, Classes, MicroXml;

procedure PrintXmlElement(Element: TMicroXmlElement; const Level: Integer);
  Indent: string;
  Index: Integer;
  Indent := StringOfChar(' ', Level * 2);
  WriteLn(Indent, '+ ', Element.Name);

  if Length(Element.Text) <> 0 then
    WriteLn(Indent, Indent, Element.Text);

  for Index := 0 to Pred(Element.AttributeCount) do
    WriteLn(Indent, Indent, Element.Attributes[Index].Name, ' = ', Element.Attributes[Index].Value);

  if (Element.ChildCount <> 0) then
    for Index := 0 to Pred(Element.ChildCount) do
      PrintXmlElement(Element.Children[Index], Level + 1);

procedure PrintXmlFile;
  Doc: TMicroXmlDocument;
  Doc := TMicroXmlDocument.Create;
    PrintXmlElement(Doc.RootElement, 0);