[ Pobierz całość w formacie PDF ]
.1, there are two new interface types: IXMLChapterTypeand IXMLSectionType, which we saw earlier in the last page of the XML Data BindingWizard, together with their _di_IXMLChapterType and _di_IXMLSectionType C++Builderinterface typecasts. 16 0672324806 CH13 12/12/02 2:38 PM Page 479XML Data Binding 479What s even more useful is the fact that the new unit also contains the class defini-tions (in BizSnap.h) and implementations (in BizSnap.cpp) for TXMLChapterType andTXMLSectionType.TXMLChapterType is derived from TXMLNodeCollection (it contains childnodes), and TXMLSectionType is derived from TXMLNode.Both implement their similar-named interface.That s not all, because the BizSnap unit also contains three global functions:GetChapter() (overloaded), LoadChapter(), and NewChapter().The first can be used toextract a Chapter from an existing TXMLDocument component or _di_XMLDocument inter-face.The second can be used to load a Chapter from an existing, compatible XMLfile (such as the BizSnap.xml file), and the NewChapter() function can be used to start anew, empty, Chapter.All three return a _di_IXMLChapterType interface, which canthen be used to work with C++Builder.As an example, let s use the GetChapter() function to extract the _di_IXMLChapterTypefrom the TXMLDocument component and work with it using the named methods andproperties.First, we must add the BizSnap header to the includes section of our mainform (in the header file of our main form), so we can use the interface types andglobal functions.Then, to use the _di_IXMLChapterType interface during the lifetime ofthe form, we can add a variable Chapter of type _di_IXMLChapterType to the form(place it in the private section).Also, add a private field called CurrentSection of typeint, and a private method called DisplaySection(), as follows:#include//                                    -class TForm1 : public TForm{__published: // IDE-managed ComponentsTXMLDocument *XMLDocument1;TMemo *Memo1;TButton *btnXML;TButton *btnFirst;TButton *btnPrev;TButton *btnNext;TButton *btnLast;void __fastcall btnXMLClick(TObject *Sender);private: // User declarations_di_IXMLChapterType Chapter;int CurrentSection;void DisplaySection(void);public: // User declarations__fastcall TForm1(TComponent* Owner);}; 16 0672324806 CH13 12/12/02 2:38 PM Page 480480 CHAPTER 13 XML Document Programming and XML MapperNow, write the following code for the OnCreate() event handler of the Form, as wellas the private method DisplaySection():void __fastcall TForm1::FormCreate(TObject *Sender){Chapter = GetChapter(XMLDocument1);CurrentSection = 0; // First SectionDisplaySection();}void TForm1::DisplaySection(void){Memo1->Lines->Clear();Memo1->Lines->Add( Chapter:  + Chapter->Title);Memo1->Lines->Add( Section:  +Chapter->Section[CurrentSection]->Title);Memo1->Lines->Add( Components:  +Chapter->Section[CurrentSection]->Components);Memo1->Lines->Add( Wizards:  +Chapter->Section[CurrentSection]->Wizards);}As you ll find out when you type along, the Code Insight features of the C++Builder6 IDE will now help us when writing code.Specifically, if you type GetChapter(), youwill be helped with the argument.When you want to use the Chapter (of type_di_IXMLChapterType), Code Insight will show you a list of available properties andmethods.No more accidental typing mistakes resulting in modified XML documentswith nonsense child nodes.Now, let s add four TButton components, called btnFirst, btnPrev, btnNext, andbtnLast.Write the following code for their OnClick event handler (to display informa-tion from the first, previous, next, and last section).void __fastcall TForm1::btnFirstClick(TObject *Sender){CurrentSection = 0;DisplaySection();}void __fastcall TForm1::btnPrevClick(TObject *Sender){if (CurrentSection){CurrentSection ; 16 0672324806 CH13 12/12/02 2:38 PM Page 481XML Data Binding 481DisplaySection();}}void __fastcall TForm1::btnNextClick(TObject *Sender){if (CurrentSection Count-1){CurrentSection++;DisplaySection();}}void __fastcall TForm1::btnLastClick(TObject *Sender){CurrentSection = Chapter->Count-1;DisplaySection();}We can now view the contents of one item of the XML document inside the TMemocontrol and use the Next and Prev buttons to navigate through the XML document(see Figure 13.8 for the application in action).FIGURE 13.8 XML Data Binding in action.As a final enhancement, we can disable the First/Prev buttons if we are showing thefirst section, and enable them otherwise [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • angela90.opx.pl