Work with XML in Delphi

Alen IBRIC
3 min readMay 11, 2023

--

A well-liked standard for storing and transmitting data between various systems is XML (Extensible Markup Language). Developers may more easily handle data in a structured manner because to Delphi’s support for working with XML files, a strong programming language. The use of XML in Delphi will be discussed in this blog article, along with some examples of reading and writing XML files. Scripts and free tools that may be used to work with XML files will also be recommended.

Delphi Reading XML Files

Delphi requires the TXMLDocument component to read XML files. This component, which is a part of Delphi’s standard library, makes it simple to read and work with XML documents. Here is an illustration of how to read an XML file using the TXMLDocument component:

uses

Xml.XMLDoc, Xml.XMLIntf;

procedure ReadXMLFile(const FileName: string);

var

XMLDoc: IXMLDocument;

Node: IXMLNode;

begin

XMLDoc := LoadXMLDocument(FileName);

Node := XMLDoc.DocumentElement;

while Assigned(Node) do

begin

// Do something with the node

Node := Node.NextSibling;

end;

end;

In this example, the XML file is loaded into memory using the LoadXMLDocument function. The DocumentElement attribute is then used to retrieve the XML document’s root element. Finally, we use the NextSibling attribute to go through each child node of the root element.

Delphi XML File Writing

Delphi’s TXMLDocument component is also required if you want to create an XML file. But this time, to add new elements to the document, you’ll utilize the AddChild method. Here is an illustration of how to create an XML file using the TXMLDocument component:

uses

Xml.XMLDoc, Xml.XMLIntf;

procedure WriteXMLFile(const FileName: string);

var

XMLDoc: IXMLDocument;

RootNode, ChildNode: IXMLNode;

begin

XMLDoc := NewXMLDocument;

RootNode := XMLDoc.AddChild(‘root’);

ChildNode := RootNode.AddChild(‘child’);

ChildNode.Attributes[‘attribute1’] := ‘value1’;

ChildNode.Attributes[‘attribute2’] := ‘value2’;

XMLDoc.SaveToFile(FileName);

end;

In this example, we first use the NewXMLDocument function to create a brand-new XML document. The AddChild function is then used to add a root element to the document. Finally, we set some attributes for the child element and add a child element to the root element. The SaveToFile function is then used to save the XML file.

Scripts and Free XML File Working Components

Working with XML files in Delphi is made possible by the availability of numerous free components and scripts. The OmniXML library, which offers a quick and lightweight XML parser that can be used to read and write XML files, is one well-liked component. The NativeXML library, which offers a more complete set of XML parsing and processing features, is another helpful library.

In addition to these libraries, there are a ton of scripts online that can be used to carry out particular XML manipulation and parsing tasks. For instance, you can find scripts that can be used to check XML documents against a schema, extract data from XML files, or alter XML documents using XSLT.

Conclusion

In conclusion, Delphi offers top-notch support for working with XML files. XML is a strong standard for storing and transmitting data between various systems. The TXMLDocument component in Delphi offers a straightforward and user-friendly interface for working with XML documents, whether you need to read data from an existing XML file or write data to a new XML file. Additionally, you can improve your Delphi’s XML processing capabilities by using a variety of free web components and scripts.

Here are some additional resources you can explore to learn more about working with XML in Delphi:

  1. The Embarcadero DocWiki — The DocWiki provides an extensive collection of articles, examples, and tutorials on Delphi and related technologies. You can find detailed documentation on the TXMLDocument component, as well as examples for working with XML in Delphi.
  2. Delphi Basics — Delphi Basics is a website that provides a large collection of code snippets and examples for Delphi programmers. You can find a variety of XML-related examples, including code for parsing and manipulating XML files.
  3. GitHub — GitHub is a popular platform for hosting and sharing code. You can find many open-source Delphi projects on GitHub that use XML, such as the OmniXML library and the NativeXML library.

Working with XML files is supported by robust functionality in Delphi. Delphi’s TXMLDocument component offers a simple and user-friendly interface for reading or writing data in an XML format. Additionally, you may expand Delphi’s support for XML processing even further by using free web components and scripts.

--

--