//--------------------------------------------------------------------------- #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 TButton *btn_1; TButton *btn_2; TButton *btn_3; TButton *btn_4; TButton *btn_5; TXMLDocument *XMLDocument1; TMemo *Memo1; 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); void __fastcall btn_5Click(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"); } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_1Click(TObject *Sender) { // XMLDocument1对象的XML属性 //u前面我们经常用这句代码,因为lines与xml同属于 TStrings Memo1->Lines= XMLDocument1->XML; //如果不怕麻烦,可以这样写 Memo1->Lines->Text=XMLDocument1->XML->Text; //如果知道了这一点,那xml属性的功能可就多了,并且这里的xl属性是可写的 //不过这里要谈的是结点的xml属性 } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_2Click(TObject *Sender) { //根节点的xml属性 _di_IXMLNode node; node=XMLDocument1->DocumentElement; //这句话有误,是因为XML是WideString类型 // Memo1->Lines=node->XML; //所以应该写成这样 Memo1->Lines->Text=node->XML; //还有一个更大的区别,节点的xml是只读的 } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_3Click(TObject *Sender) { //子节点的xml属性 _di_IXMLNode node; node=XMLDocument1->DocumentElement->ChildNodes->Nodes[0]; Memo1->Lines->Text=node->XML;//会显示一个子结点的全部 } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_4Click(TObject *Sender) { //属性的xml属性 _di_IXMLNode node; node=XMLDocument1->DocumentElement; node=node->AttributeNodes->Nodes[0]; ShowMessage(node->NodeName);//备注 ShowMessage(node->NodeValue);//测试 //用xml属性一次把他们都读出来 ShowMessage(node->XML); } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_5Click(TObject *Sender) { //叶节点的xml属性 _di_IXMLNode node; node=XMLDocument1->DocumentElement; node=node->ChildNodes->Nodes[0]; node=node->ChildNodes->Nodes[0]; node=node->ChildNodes->Nodes[0];//这就是叶节点 ShowMessage(node->XML); //这时的xml属性和text属性一样了 ShowMessage(node->Text); } //---------------------------------------------------------------------------
时间: 2024-09-27 10:59:17