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

简介:

测试用的xml

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

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

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

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

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

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

</科室名单>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//---------------------------------------------------------------------------
 
#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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//---------------------------------------------------------------------------
 
#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();
}
//--------------------------------------------------------------------------
相关文章
|
15天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
60 30
|
4天前
|
并行计算 Unix Linux
超级好用的C++实用库之线程基类
超级好用的C++实用库之线程基类
12 4
|
4天前
|
C++ Windows
HTML+JavaScript构建C++类代码一键转换MASM32代码平台
HTML+JavaScript构建C++类代码一键转换MASM32代码平台
|
4天前
|
C++
2合1,整合C++类(Class)代码转换为MASM32代码的平台
2合1,整合C++类(Class)代码转换为MASM32代码的平台
|
4天前
|
存储 运维 监控
超级好用的C++实用库之日志类
超级好用的C++实用库之日志类
10 0
|
29天前
|
存储 编译器 C++
C ++初阶:类和对象(中)
C ++初阶:类和对象(中)
|
2月前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
29天前
|
C++
C++(十六)类之间转化
在C++中,类之间的转换可以通过转换构造函数和操作符函数实现。转换构造函数是一种单参数构造函数,用于将其他类型转换为本类类型。为了防止不必要的隐式转换,可以使用`explicit`关键字来禁止这种自动转换。此外,还可以通过定义`operator`函数来进行类型转换,该函数无参数且无返回值。下面展示了如何使用这两种方式实现自定义类型的相互转换,并通过示例代码说明了`explicit`关键字的作用。
|
29天前
|
存储 设计模式 编译器
C++(十三) 类的扩展
本文详细介绍了C++中类的各种扩展特性,包括类成员存储、`sizeof`操作符的应用、类成员函数的存储方式及其背后的`this`指针机制。此外,还探讨了`const`修饰符在成员变量和函数中的作用,以及如何通过`static`关键字实现类中的资源共享。文章还介绍了单例模式的设计思路,并讨论了指向类成员(数据成员和函数成员)的指针的使用方法。最后,还讲解了指向静态成员的指针的相关概念和应用示例。通过这些内容,帮助读者更好地理解和掌握C++面向对象编程的核心概念和技术细节。
|
2月前
|
存储 算法 编译器
c++--类(上)
c++--类(上)