RAD Studio for Microsoft .NET |  |

Building an Application with XML Components
See Also
Collapse All

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.

Copy Code

    <?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>Borland</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. Start a new project.
2. Choose FileNewOther.
3. In the New Items dialog box, select Delphi for .NET Projects.
4. Double-click VCL Forms Application. The VCL Forms Designer displays.
5. From the Internet category on the Tool Palette, place an TXMLDocument component on the form.
6. 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.
7. 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 Borland 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. Select Button1.
2. In the Object Inspector double-click the OnClick event on the Events tab. The code displays with the cursor in the TForm1.Button1Click event handler block.
3. Enter the following code to display the stock price for the first child node when the Borland button is clicked:

Copy Code

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

1. Add a var section just above the code block, above the begin statement in the event handler, and enter the following local variable declarations:

Copy Code

    var
         BorlandStock: IXMLNode;
      Price: string;

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

Copy Code

    MyCompany:=XMLDocument1.DocumentElement.ChildNodes[1];
    Price:= MyCompany.ChildNodes['price'].Text;
    Memo1.Text := Price;

1. Add a var section just above the code block, above the begin statement in the event handler, and enter the following local variable declarations:

Copy Code

    var
      MyCompany: IXMLNode;
      Price: string;

To compile and run the application

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

See Also

VCL.NET Overview

Building a VCL Forms Application

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!