Borland
C++ Builder 6 Examples
Example
#1: Generate XML
This example
generates three different XML files.
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CHILKATXMLLib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// Unlock the component.
IChilkatXmlPtr xml = XmlFactory1->NewXml();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
IChilkatXmlPtr xml = XmlFactory1->NewXml();
xml->set_Tag(WideString("myTag"));
xml->set_Content(WideString("This is content"));
xml->SaveXML(WideString("data1.xml"));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
IChilkatXmlPtr xml = XmlFactory1->NewXml();
xml->set_Tag(WideString("sample"));
int i;
for (i=0; i<100; i++)
{
IChilkatXmlPtr xml2 = xml->NewChild(WideString("record"),WideString(""));
xml2->NewChild(WideString("first_name"),WideString("Matt"));
xml2->NewChild(WideString("last_name"),WideString("Fausey"));
xml2->NewChild(WideString("company"),WideString("Chilkat Software, Inc."));
xml2->NewChild(WideString("website"),WideString("http://www.chilkatsoft.com"));
xml2->NewChild(WideString("email"),WideString("fausey@chilkatsoft.com"));
}
xml->SaveXML(WideString("data2.xml"));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
IChilkatXmlPtr xml = XmlFactory1->NewXml();
xml->set_Tag(WideString("sample"));
int i;
for (i=0; i<100; i++)
{
IChilkatXmlPtr xml2 = xml->NewChild(WideString("record"),WideString(""));
xml2->NewChild(WideString("first_name"),WideString("Matt"));
IChilkatXmlPtr xml3 = xml2->NewChild(WideString("last_name"),WideString("Fausey"));
xml3->AddAttribute(WideString("favorite_color"),WideString("blue"));
xml3->AddAttribute(WideString("favorite_beverage"),WideString("coffee"));
xml3 = xml2->NewChild(WideString("company"),WideString("Chilkat Software, Inc."));
xml3->AddAttribute(WideString("headquarters"),WideString("Chicago"));
xml2->NewChild(WideString("website"),WideString("http://www.chilkatsoft.com"));
xml2->NewChild(WideString("email"),WideString("fausey@chilkatsoft.com"));
}
xml->SaveXML(WideString("data3.xml"));
}
//---------------------------------------------------------------------------
Example
#2: Navigate XML
This example
loads an XML file containing the Shakespeare play "Hamlet"
and demonstrates how to navigate around the file and display some
information. The data file as well as source and project files
are included in the Chilkat XML download.
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CHILKATXMLLib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
// Unlock the component.
IChilkatXmlPtr xml = XmlFactory1->NewXml();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// Open hamlet.xml
IChilkatXmlPtr xml = XmlFactory1->NewXml();
xml->LoadXmlFile(WideString("hamlet.xml"));
// Find the play"s title.
IChilkatXmlPtr after; // Leave this empty.
IChilkatXmlPtr xml2 = xml->SearchForTag(after,WideString("TITLE"));
Memo1->Lines->Append(xml2->get_Content());
Memo1->Lines->Append(" ");
// Find the courtiers group: courtiers.
// and then print out each of them.
xml2 = xml->SearchForContent(after,WideString("GRPDESCR"),WideString("*courtier*"));
// Move up one level to the parent.
xml2 = xml2->GetParent();
// Step through each tag.
xml2 = xml2->FirstChild();
while (xml2.IsBound())
{
AnsiString tempStr = AnsiString(xml2->get_Tag());
if (tempStr == "PERSONA" )
{
Memo1->Lines->Append("courtier: " + AnsiString(xml2->get_Content()));
}
xml2 = xml2->NextSibling();
}
Memo1->Lines->Append(" ");
// Find the part in the play containing this phrase:
// "There are more things in heaven and earth" and then display
// the entire passage.
xml2 = xml->SearchAllForContent(after,WideString("*There are more things in heaven and earth*"));
xml2 = xml2->GetParent();
xml2 = xml2->FirstChild();
while (xml2.IsBound())
{
AnsiString tempStr = AnsiString(xml2->get_Tag());
if (tempStr == "LINE" )
{
Memo1->Lines->Append(AnsiString(xml2->get_Content()));
}
xml2 = xml2->NextSibling();
}
Memo1->Lines->Append(" ");
// Count the number of times Hamlet speaks.
xml2 = xml->SearchForContent(after,WideString("SPEAKER"),WideString("HAMLET"));
int count = 0;
while (xml2.IsBound())
{
count = count + 1;
IChilkatXmlPtr xml3 = xml2->NextSibling();
Memo1->Lines->Append(AnsiString(xml3->get_Content()));
xml2 = xml->SearchForContent(xml2,WideString("SPEAKER"),WideString("HAMLET"));
}
Memo1->Lines->Append(" ");
Memo1->Lines->Append("Hamlet speaks "+IntToStr(count)+" times");
}
//---------------------------------------------------------------------------
|