Building an Application with XML Components

From RAD Studio
Jump to: navigation, search

Go Up to How To Build VCL Forms Applications


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>MyAppDevCo</name>
    <price>10.375</price>
    <symbol>MADCO</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 File > New > Other > Delphi 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 MyAppDevCo 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 MyAppDevCo button is clicked:
MyAppDevCoStock:=XMLDocument1.DocumentElement.ChildNodes[0];
Price:= MyAppDevCoStock.ChildNodes['price'].Text;
Memo1.Text := Price;
For C++
IXMLNode *MyAppDevCoStock = XMLDocument1->DocumentElement->ChildNodes->GetNode(0);
WideString Price = MyAppDevCoStock->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:
  2. var
      MyAppDevCoStock: IXMLNode;
      Price: string;
    
  3. 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.
  4. Enter the following code to display the stock price for the second child node when the MyCompany button is clicked:
  5. MyCompany:=XMLDocument1.DocumentElement.ChildNodes[1];
    Price:= MyCompany.ChildNodes['price'].Text;
    Memo1.Text := Price;
    
    For C++
    IXMLNode *MyCompany = XMLDocument1->DocumentElement
          ->ChildNodes->GetNode(1);
    WideString Price = MyAppDevCoStock->ChildNodes
          ->FindNode( "price" )->Text;
    Memo1->Text = Price;
    
  6. 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 Run > Run to compile and execute the application.The application form displays two buttons and a memo.
  2. Click the MyAppDevCo button. The stock price displays.
  3. Click the MyCompany button.The stock price displays.

See Also