//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <msxmldom.hpp> #include <XMLDoc.hpp> #include <xmldom.hpp> #include <XMLIntf.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TXMLDocument *XMLDocument1; TButton *btn_1; TButton *btn_2; TButton *btn_3; TButton *btn_4; void __fastcall FormCreate(TObject *Sender); void __fastcall btn_1Click(TObject *Sender); void __fastcall btn_2Click(TObject *Sender); void __fastcall btn_3Click(TObject *Sender); void __fastcall btn_4Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { XMLDocument1->LoadFromFile("D:\\code\\B_C_B\\XMLDocument\\test.xml"); } //--------------------------------------------------------------------------- /* 什么是支节点与叶节点 没有权威的参考,我的理解是: 包含其他标签的节点是支节点,不包含其他标签的就是叶节点,文本结点不算叶结点 比如根节点下面的第一个节点: <人员 职务=“科长” 备注=“正局级”> <姓名>张三</姓名> <性别>男</性别> <年龄>34</年龄> </人员> "“人员”节点是支结点 "姓名" 、“姓别" " 年龄”是叶节点 叶节点中的部分是文本节点 */ //--------------------------------------------------------------------------- void __fastcall TForm1::btn_1Click(TObject *Sender) { //判断是否是叶节点 _di_IXMLNode node; node=XMLDocument1->DocumentElement->ChildNodes->Nodes[0]; if(node->HasChildNodes&&!(node->IsTextElement)) ShowMessage("是支节点"); } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_2Click(TObject *Sender) { //判断是不是叶节点 _di_IXMLNode node; node=XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->ChildNodes->Nodes[0]; if(node->IsTextElement) { ShowMessage("是叶节点"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_3Click(TObject *Sender) { //是不是文本节点 _di_IXMLNode node; node=XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->ChildNodes->Nodes[0]->ChildNodes->Nodes[0]; //这个张三是文本节点 if((node->NodeName)==WideString("#text")) ShowMessage("文本结点"); } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_4Click(TObject *Sender) { //判断是否为空结点 _di_IXMLNode node; //这是根节点的第五个节点,“辅助人员”,xml中设为空 node=XMLDocument1->DocumentElement->ChildNodes->Nodes[4]; if((!(node->HasChildNodes))&&(!node->IsTextElement)) ShowMessage("空结点"); } //---------------------------------------------------------------------------