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();
}
//--------------------------------------------------------------------------
相关文章
|
26天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
33 0
|
26天前
|
存储 编译器 C语言
C++入门: 类和对象笔记总结(上)
C++入门: 类和对象笔记总结(上)
32 0
|
1天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
11 0
|
1天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
10 0
|
4天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
6天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
6天前
|
编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(中)——“C++”
|
6天前
|
存储 编译器 C++
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
自从学了C++之后,小雅兰就有对象了!!!(类与对象)(上)——“C++”
|
7天前
|
C++
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元
【C++成长记】C++入门 | 类和对象(下) |Static成员、 友元