c++ builder 中的 XMLDocument 类详解(5) - 获取元素内容

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

#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
 
//---------------------------------------------------------------------------

#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);//分别显示年龄
    }

}
//---------------------------------------------------------------------------
时间: 2024-10-23 13:07:51

c++ builder 中的 XMLDocument 类详解(5) - 获取元素内容的相关文章

c++ builder 中的 XMLDocument 类详解(7) - 获取节点名称与节点的属性名称

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &

c++ builder 中的 XMLDocument 类详解(9) -关于 HasChildNodes 与 IsTextElement

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &

c++ builder 中的 XMLDocument 类详解(10) -判断节点类型: 支节点、叶节点、文本节点、空节点

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &

C++ builder 中的 XMLDocument 类详解(15) – Firstnode 和Lastnode

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &

C++ builder 中的 XMLDocument 类详解(16) – 前一个节点、后一个节点和父节点

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &

C++ builder 中的 XMLDocument 类详解(17) 更好地显示 xml 的测试结果 - FormatXMLData 及其他

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &

c++ builder 中的 XMLDocument 类详解(8) -添加与删除结点

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &

c++ builder 中的 XMLDocument 类详解(6) - 访问节点属性

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &

c++ builder 中的 XMLDocument 类详解(12) -关于 XML 属性

//--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include &