Wątki

[ Pobierz całość w formacie PDF ]
.This is still asomewhat DOM-like interface, but a bit more powerful and easier to use, as I ll showin this section.The interfaces IXMLDocument, IXMLNode, IXMLNodeList, and IXMLNodeCollection are defined in the XMLIntf.pas unit (and typecast in XMLIntf.hpp to_di_IXMLDocument, _di_IXMLNode, _di_IXMLNodeList, and _di_IXMLNodeCollection).Although _di_IDOMDocument is also available through the DOMDocument property, the_di_IXMLDocument is an easier and the preferred way (in C++Builder 6) to work withthe TXMLDocument component, so let s examine the _di_IXMLDocument interface in somemore detail now.Reading XML DocumentsTo put the XML document programming theory into practice, let s now build anexample application using the TXMLDocument component.Start a new Application,save the main form in the default Unit1, and the project in Project1.Drop aTXMLDocument component next to a TMemo and TButton component.Set the Captionproperty of the TButton component to XML Doc and its Name to btnXMLDoc (seeFigure 13.1).FIGURE 13.1 TXMLDocument component at design time.Connect the FileName property of the TXMLDocument component to the BizSnap.xml file,and set the doAutoSave flag of the Options property to true.Now, we can open theTXMLDocument by setting the Active property to true as well.After the TXMLDocument isactive, we can access the root and traverse through the hierarchy of nodes, readingand writing values, adding nodes, and more.Each node in the hierarchy is of type_di_IXMLNode.We can traverse through the nodes in this hierarchy and use the TMemo component todisplay the nodes and their attributes we encounter along the way.First of all, theroot node can be obtained using the DocumentElement property.After we have theroot, we can get the attributes as well as child nodes.The following code for theOnClick event handler of btnXMLDoc will get the root node, the attribute with nameTitle, followed by the first child node with attributes Title, and child nodesComponents, and Wizards. 16 0672324806 CH13 12/12/02 2:38 PM Page 471XML Document Programming 471void __fastcall TForm1::btnXMLDocClick(TObject *Sender){Memo1->Lines->Clear();_di_IXMLNode Chapter = XMLDocument1->DocumentElement;Memo1->Lines->Add( Chapter:  + Chapter->Attributes[ Title ]);_di_IXMLNode Section = Chapter->ChildNodes->GetNode(0);Memo1->Lines->Add( Section:  + Section->Attributes[ Title ]);Memo1->Lines->Add( Components:  +Section->ChildNodes->Nodes[Section->ChildNodes->IndexOf( Components )]->GetText());Memo1->Lines->Add( Wizards:  +Section->ChildNodes->Nodes[(AnsiString) Wizards ]->GetText());}Note that the Nodes property can be indexed with a name or index.If you want touse a name, however, you need to explicitly cast it to an AnsiString first.The output of clicking the XMLDoc button can be seen in Figure 13.2.FIGURE 13.2 XMLDocument Component at runtime.Note that we have to make sure to give the exact names of the attributes and childnodes.If we supply an incorrect name for an attribute, for example, we get an excep-tion of class EVariantTypeCastError telling us that an invalid variant type conversionwas attempted.An incorrect name for a child node results in an empty node, andhence, no value for the Text property.In other words, we should be careful not tomake accidental typing mistakes.For each node, we can check the HasChildNodes property to make sure it indeedcontains any child nodes.The ChildNodes->Count property contains the number ofchild nodes, just as the AttributeNodes->Count contains the number of attributes.Andfinally, each node can return its Text as well as XML representation.Writing XML DocumentsApart from browsing through an XML document, we can also modify and save theupdated XML document.In fact, it s so easy to change the XML Document thatsometimes you ve already done it without even knowing it.Remember how I told 16 0672324806 CH13 12/12/02 2:38 PM Page 472472 CHAPTER 13 XML Document Programming and XML Mapperyou about the doAutoSave option; this will make sure the contents of the TXMLDocumentcomponent is automatically saved whenever we make a change to it.Anotheroption, the doNodeAutoCreate, will make sure that if we try to access a node thatdoesn t exist yet, it will dutifully create one for us.This flag is set to true by default [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • mikr.xlx.pl
  • Powered by MyScript