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
|
//---------------------------------------------------------------------------
#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;
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);
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
//---------------------------------------------------------------------------
#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"
);
Memo1->Lines=XMLDocument1->XML;
}
//---------------------------------------------------------------------------
void
__fastcall TForm1::btn_1Click(TObject *Sender)
{
//访问第一个人的信息
ShowMessage(XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->ChildNodes->FindNode(
"姓名"
)->Text);
// 张三
ShowMessage(XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->ChildNodes->FindNode(
"性别"
)->Text);
// 男
ShowMessage(XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->ChildNodes->FindNode(
"年龄"
)->Text);
// 34
//可以通过位置访问子节点,如:ChildNodes->Nodes[0] 表示子节点列表中的第一个元素
// 可以通过子节点名访问子节点, 如: ChildNodes->['姓名']
//但在节点名一样的情况下, 只能通过位置访问}
//任何情况下都可以通过位置访问, 譬如:
//ShowMessage(XMLDocument1->DocumentElement->ChildNodes->Nodes[0]->ChildNodes->nodes[0]->Text);// 张三
}
//---------------------------------------------------------------------------
void
__fastcall TForm1::btn_2Click(TObject *Sender)
{
//访问第二个人的信息
_di_IXMLNodeList nodeList;
nodeList=XMLDocument1->DocumentElement->ChildNodes;
_di_IXMLNode node=nodeList->Nodes[1];
ShowMessage(node->ChildNodes->FindNode(
"姓名"
)->Text);
ShowMessage(node->ChildNodes->FindNode(
"性别"
)->Text);
ShowMessage(node->ChildNodes->FindNode(
"年龄"
)->Text);
}
//---------------------------------------------------------------------------
void
__fastcall TForm1::btn_3Click(TObject *Sender)
{
//获得所有成员
_di_IXMLNodeList nodeList;
_di_IXMLNode node;
nodeList=XMLDocument1->DocumentElement->ChildNodes;
for
(
int
i=0;i<nodeList->Count-1;i++)
//这里一定是nodelist->count-1
{
node=nodeList->Nodes[i];
ShowMessage(node->ChildNodes->FindNode(
"姓名"
)->Text);
//分别显示姓名
}
}
//---------------------------------------------------------------------------
void
__fastcall TForm1::btn_4Click(TObject *Sender)
{
//获取所有成员年龄
_di_IXMLNodeList nodeList;
_di_IXMLNode node;
nodeList=XMLDocument1->DocumentElement->ChildNodes;
for
(
int
i=0;i<nodeList->Count-1;i++)
//这里一定是nodelist->count-1
{
node=nodeList->Nodes[i];
ShowMessage(node->ChildNodes->FindNode(
"年龄"
)->Text);
//分别显示年龄
}
}
//---------------------------------------------------------------------------
|