Last Updated:

Load and parse an XML document.

The exchange of information in XML format is an extremely convenient mechanism to minimize the problems of in-house data formats. These problems boil down to difficulties in exchanging information with the firm's counterparties. Often the problem is not only in the inability of others to perceive the format you propose (DBF, ASCII, etc.), but also in the unwillingness to adapt to them. These formats are not very convenient. In addition, your partner probably already has developments in this area. So why don't you adjust to its data interchange format?

The XML language allows you to develop data description formats that can become the bridge that will connect all counterparties and eliminate the technical and psychological barriers that inevitably arise when adapting to foreign technologies.

XML technology can also not be called native, but its capabilities and versatility in describing text data overlap all the difficulties associated with its development.

Forming documents in XML format is quite simple. You just need to get acquainted with the specific DTD and samples of correct documents. But the download can be quite difficult if you do not resort to the help of ready-made solutions in the form of XML parsers. There are quite a few of them for different platforms and if you wish, you can find their descriptions on the WWW. One of the most common on the Windows platform is Microsoft XML Parser. The fact is that it is included in Microsoft Explorer 5.0 and later versions. It is available as an ActiveX object. This parser is a verifier, that is, it checks not only the syntax check of the document, but also the semantic correctness in accordance with the specified DTD.

To load and analyze a document in Delphi, consider the following code.

procedure LoadOnixDoc(const FileName: string);
  XML: variant;
  Node, mainNode, childNodes: variant;
  i: integer;
  TreeNode: TTreeNode;

  procedure LoadItems(TreeNode: TTreeNode; Node: variant);
  var i: integer;
    TreeNode := TV.Items.AddChild(TreeNode, Node.nodeName);
    TreeNode.ImageIndex := TreeNode.Level;
    TreeNode.SelectedIndex := TreeNode.ImageIndex;
    if Node.nodeName = '#text' then
      TreeNode.Text := Node.nodeValue;
      TV.SetNodeBoldState(TreeNode, true);
    for i:=0 to Node.childNodes.length-1 do
     LoadItems(TreeNode, Node.childNodes.item[i]);

  XML := CreateOleObject('Microsoft.XMLDOM');

  if XML.parseError.reason <> '' then
    ShowMessage( XML.parseError.reason );
  end else

    mainNode := XML.documentElement;
    childNodes := mainNode.childNodes;
    LoadItems(nil, mainNode);
    TreeNode := TV.Items[1];
    while Assigned(TreeNode) do
      TreeNode := TreeNode.GetNextSibling;
    if Assigned(TV.Items[0]) then TV.Items[0].Expand(false);


An example of an Onix XML document.

<?xml version="1.0" encoding="windows-1251"?>
<!DOCTYPE ONIXMessage SYSTEM "onix-international.dtd">

<ONIXMessage release="1.1">

    <FromCompany>TD Biblio-Globus</FromCompany>
    <FromPerson>Kirillov Alexey kirillov@biblio-globus.u</FromPerson>
    <ToPerson>EDItEUR chief manager</ToPerson>
        <DistinctiveTitle>SQL user encyclopedia</DistinctiveTitle>
            <PersonName>X. Incense</PersonName>
            <PersonNameInverted>Incense, H.</PersonNameInverted>
            <BiographicalNote>The book covers topics that are often
             are not described or briefly presented in
             user manuals
             - subqueries, data structures, views,
             performance, integrity and data protection.


As a result, we will form the following document tree: