RAD Studio
ContentsIndex
PreviousUpNext
Building an Application with XML Components

This example creates a VCL Forms application that uses an XMLDocument component to display contents in an XML file.

The basic steps are:

  1. Create an XML document.
  2. Create a VCL form.
  3. Place an XMLDocument component on the form, and associate it with the XML file.
  4. Create VCL components to enable the display of XML file contents.
  5. Write event handlers to display XML child node contents.
  6. Compile and run the application.

To create the XML document

  1. Copy the text below into a file in a text editor.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE StockHoldings [
    <!ELEMENT StockHoldings (Stock+)>
    <!ELEMENT Stock (name)>
    <!ELEMENT Stock (price)>
    <!ELEMENT Stock (symbol)>
    <!ELEMENT Stock (shares)>
]>

<StockHoldings>
  <Stock exchange="NASDAQ">
    <name>CodeGear</name>
    <price>10.375</price>
    <symbol>BORL</symbol>
    <shares>100</shares>
  </Stock>

  <Stock exchange="NYSE">
    <name>MyCompany</name>
    <price>8.75</price>
    <symbol>MYCO</symbol>
    <shares type="preferred">25</shares>
  </Stock>
</StockHoldings>

  1. Save the file to your local drive as an XML document. Give it a name such as stock.xml.
  2. Open the document in your browser. The contents should display without error.
    Note: In the browser, you can choose View->Source
    to view the source file in the text editor.

To create a form with an XMLDocument component

  1. Choose FileNewOtherDelphi Projects or C++Builder Projects and double-click the VCL Forms Application icon. The VCL Forms Designer is displayed.
  2. From the Internet page on the Tool Palette, place an TXMLDocument component on the form.
  3. In the Object Inspector, click the ellipsis button next to the FileName property, browse to the location of the XML file you created, and open it. The XML file is associated with the TXMLDocument component.
  4. In the Object Inspector, set the Active property to true.

To set up the VCL components

  1. From the Standard page on the Tool Palette, place a TMemo component on the form.
  2. From the Standard page on the Tool Palette, place two TButton components on the form just above Memo1.
  3. In the Object Inspector with Button1 selected, enter CodeGear for the Caption property.
  4. In the Object Inspector with Button2 selected, enter MyCompany for the Caption property.

To display child node contents in the XML file

  1. In the Object Inspector with Button1 selected, double-click the OnClick event on the Events tab. The Code displays with the cursor in the TForm1.Button1Click event handler block.
  2. Enter the following code to display the stock price for the first child node when the CodeGear button is clicked:

  BorlandStock:=XMLDocument1.DocumentElement.ChildNodes[0];
  Price:= BorlandStock.ChildNodes['price'].Text;
  Memo1.Text := Price;

 

IXMLNode *BorlandStock = XMLDocument1->DocumentElement->
            ChildNodes->GetNode(0);
WideString Price = BorlandStock->ChildNodes->
            FindNode( "price" )->Text;
Memo1->Text = Price;

  1. For Delphi, add a var section just above the code block in the event handler, and enter the following local variable declarations:

var
     BorlandStock: IXMLNode;
  Price: string;

  1. In the Object Inspector with Button2 selected, double-click the OnClick event on the Events tab. The Code displays with the cursor in the TForm1.Button2Click event handler block.
  2. Enter the following code to display the stock price for the second child node when the MyCompany button is clicked:

  MyCompany:=XMLDocument1.DocumentElement.ChildNodes[1];
  Price:= MyCompany.ChildNodes['price'].Text;
  Memo1.Text := Price;
IXMLNode *MyCompany = XMLDocument1–>DocumentElement
       ->ChildNodes->GetNode(1);
WideString Price = BorlandStock->ChildNodes
       ->FindNode( “price” )->Text;
Memo1–>Text = Price;

  1. For Delphi, add a var section just above the code block in the event handler, and enter the following local variable declarations:

var
  MyCompany: IXMLNode;
  Price: string;
To compile and run the application

  1. Choose RunRun to compile and execute the application. The application form displays two buttons and a memo.
  2. Click the CodeGear button. The stock price displays.
  3. Click the MyCompany button. The stock price displays.

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!