c++ builder中的 XMLDocument 类详解(1) - 读取xml
时间:2011-06-08 来源:my blog
#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; TMemo *Memo1; TButton *btn_2; TButton *btn_3; TButton *btn_4; TButton *btn_5; void __fastcall btn_1Click(TObject *Sender); void __fastcall btn_2Click(TObject *Sender); void __fastcall btn_5Click(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::btn_1Click(TObject *Sender) { //载入方法1 XMLDocument1->LoadFromFile("D:\\code\\B_C_B\\XMLDocument\\test.xml"); Memo1->Lines=XMLDocument1->XML;//查看 } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_2Click(TObject *Sender) { //载入方法2 XMLDocument1->FileName="D:\\code\\B_C_B\\XMLDocument\\test.xml"; XMLDocument1->Active=true;//激活 Memo1->Lines=XMLDocument1->XML; } //--------------------------------------------------------------------------- void __fastcall TForm1::btn_3Click(TObject *Sender) { //载入方法3 TMemoryStream *ms=new TMemoryStream(); ms->LoadFromFile("D:\\code\\B_C_B\\XMLDocument\\test.xml"); XMLDocument1->LoadFromStream(ms); ms->Free(); Memo1->Lines=XMLDocument1->XML; } void __fastcall TForm1::btn_4Click(TObject *Sender) { //可以loadFromFile或指定fileName访问网上的xml XMLDocument1->LoadFromFile("http://xtayaitak.free.yun110.cn/test.xml");//这个网址 Memo1->Lines=XMLDocument1->XML;//查看 //============================================================================== // 如果需要用浏览器查看 xml, 需要一个 api 函数: ShellAPI.ShellExecute, 所以先 uses ShellAPI; //然后: ShellExecute(Handle, 'open', 'c:\temp\test.xml', nil, nil, SW_NORMAL); //============================================================================== } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TForm1::btn_5Click(TObject *Sender) { Memo1->Clear(); } //--------------------------------------------------------------------------
相关阅读 更多 +