c++ builder 中的 XMLDocument 类详解(3) 读取xml

测试用的xml

<?xml version="1.0" encoding="gb2312"?>
<科室名单 备注="测试">

  <人员 职务="科长" 备注="正局级">
    <姓名>张三</姓名>
    <性别>男</性别>
    <年龄>34</年龄>
  </人员>

  <人员 职务="付科长">
    <姓名>李四</姓名>
    <性别>女</性别>
    <年龄>43</年龄>
  </人员>

  <人员>
    <姓名>王五</姓名>
    <性别>女</性别>
    <年龄>25</年龄>
  </人员>

  <人员>
    <姓名>孙六</姓名>
    <性别>男</性别>
    <年龄>52</年龄>
  </人员>

  <辅助人员></辅助人员>

</科室名单>

//---------------------------------------------------------------------------

#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();
}
//--------------------------------------------------------------------------

本人新博客网址为:http://www.hizds.com
本博客注有“转”字样的为转载文章,其余为本人原创文章,转载请务必注明出处或保存此段。c++/lua/windows逆向交流群:69148232
原文地址:https://www.cnblogs.com/zhangdongsheng/p/2075774.html