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);
var
  XML: variant;
  Node, mainNode, childNodes: variant;
  i: integer;
  TreeNode: TTreeNode;

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

  end;
begin 
  XML := CreateOleObject('Microsoft.XMLDOM');
  XML.load(FileName);

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

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

  end;
end;

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>
    <ToCompany>EDItEUR</ToCompany>
    <ToPerson>EDItEUR chief manager</ToPerson>
    <MessageNumber>1</MessageNumber>
    <SentDate>12/23/00</SentDate>
    <DefaultLanguageOfText>rus</DefaultLanguageOfText>
    <product>
        <RecordReference>247825</RecordReference>
        <NotificationType>3</NotificationType>
        <ISBN>966-7393-05-4</ISBN>
        <ProductForm>BB</ProductForm>
        <DistinctiveTitle>SQL user encyclopedia</DistinctiveTitle>
        <Contributor>
            <ContributorSequenceNumber>0</ContributorSequenceNumber>
            <ContributorRole>A01</ContributorRole>
            <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.
            </BiographicalNote>
        </Contributor>
        <NumberOfPages>624</NumberOfPages>
        <ImprintName>DiaSoft</ImprintName>
        <MediaFile>
            <MediaFileTypeCode>04</MediaFileTypeCode>
            <MediaFileFormatCode>03</MediaFileFormatCode>
            <MediaFileLinkTypeCode>06</MediaFileLinkTypeCode>
            <MediaFileLink>
            http://shop.biblio-globus.ru/photos1/05/58983.jpg
            </MediaFileLink>
        </MediaFile>
        <SupplyDetail>
            <SupplierName></SupplierName>
            <AvailabilityCode></AvailabilityCode>
            <price>
                <PriceTypeCode>01</PriceTypeCode>
                <PriceAmount>425</PriceAmount>
                <CurrencyCode>RUB</CurrencyCode>
            </Price>
        </SupplyDetail>
    </Product>

</ONIXMessage>

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

 

document-tree