C++ Builder 中操作XML文件

                                                    C++ Builder 中操作XML文件 

在C++ Builder中操作XML文件这方面的文章,我在网上找了一下不是很多。有一篇是介绍直接操作DOM节点的方法操作XML文件。这种方法对XML文件的层次操作的时候显得特别复杂。这里介绍一个比较简单的方法。首先我们定义一个XML文件的Schema或者DTD。然后在我们程序里边添加一个TXMLDocument控件。然后双击这个控件弹出绑定向导。第一步选择刚才创建的Shema或者DTD文件。第二步显示向导为我们Shema或者DTD里边定义的元素生成的操作全局函数。第三步显示接口的代码预览。确认后生成我们的Shema或者DTD的包装文件。然后我们操作这个XML的时候直接使用这个文件里边的接口会十分的方便
   下边我们用一个描述电子邮件shema为例子来说明。shema的代码如下:
 
email.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XML Spy v4.3 U (http://www.xmlspy.com) by ZSL (YSS) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
 <xs:complexType name="BodyType">
  <xs:sequence>
   <xs:element name="text" type="xs:string"/>
   <xs:element name="attachment" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name="HeadType">
  <xs:sequence>
   <xs:element name="from" type="xs:string"/>
   <xs:element name="to" type="xs:string"/>
   <xs:element name="Subject" type="xs:string"/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name="emailType">
  <xs:sequence>
   <xs:element name="head" type="HeadType"/>
   <xs:element name="Body" type="BodyType"/>
  </xs:sequence>
 </xs:complexType>
 <xs:element name="email" type="emailType">
  <xs:annotation>
   <xs:documentation>Comment describing your root element</xs:documentation>
  </xs:annotation>
 </xs:element>
</xs:schema>

这个Shema用任意一个工具都可以生很简单的生成。下边是生成的包装文件的代码。

email.h
// ****************************************************** //
//                                                     
//                   XML Data Binding                  
//                                                     
//         Generated on: 2006-8-11 15:26:34            
//       Generated from: D:/My document/xml/email.xsd  
//   Settings stored in: D:/My document/xml/email.xdb  
//                                                     
// ****************************************************** //

#ifndef   emailH
#define   emailH

#include <System.hpp>
#include <xmldom.hpp>
#include <XMLDoc.hpp>
#include <XMLIntf.hpp>
#include <XMLNodeImp.h>


// Forward Decls

__interface IXMLemailType;
typedef System::DelphiInterface<IXMLemailType> _di_IXMLemailType;
__interface IXMLHeadType;
typedef System::DelphiInterface<IXMLHeadType> _di_IXMLHeadType;
__interface IXMLnameAddressType;
typedef System::DelphiInterface<IXMLnameAddressType> _di_IXMLnameAddressType;
__interface IXMLBodyType;
typedef System::DelphiInterface<IXMLBodyType> _di_IXMLBodyType;
__interface IXMLattachment;
typedef System::DelphiInterface<IXMLattachment> _di_IXMLattachment;
__interface IXMLattachmentList;
typedef System::DelphiInterface<IXMLattachmentList> _di_IXMLattachmentList;

// IXMLemailType

__interface INTERFACE_UUID("{720E005C-A031-4C58-BA0A-511F20A89099}") IXMLemailType : public IXMLNode
{
public:
  // Property Accessors
  virtual _di_IXMLHeadType __fastcall Get_head() = 0;
  virtual _di_IXMLBodyType __fastcall Get_Body() = 0;
  // Methods & Properties
  __property _di_IXMLHeadType head = { read=Get_head };
  __property _di_IXMLBodyType Body = { read=Get_Body };
};

// IXMLHeadType

__interface INTERFACE_UUID("{6A33C386-3111-42AF-8F07-C948A37C38D5}") IXMLHeadType : public IXMLNode
{
public:
  // Property Accessors
  virtual _di_IXMLnameAddressType __fastcall Get_from() = 0;
  virtual _di_IXMLnameAddressType __fastcall Get_to() = 0;
  virtual WideString __fastcall Get_Subject() = 0;
  virtual void __fastcall Set_Subject(WideString Value) = 0;
  // Methods & Properties
  __property _di_IXMLnameAddressType from = { read=Get_from };
  __property _di_IXMLnameAddressType to = { read=Get_to };
  __property WideString Subject = { read=Get_Subject, write=Set_Subject };
};

// IXMLnameAddressType

__interface INTERFACE_UUID("{08E2078E-BBFC-4808-9D58-7F0D6859E34C}") IXMLnameAddressType : public IXMLNode
{
public:
  // Property Accessors
  virtual WideString __fastcall Get_address() = 0;
  virtual WideString __fastcall Get_name() = 0;
  virtual void __fastcall Set_address(WideString Value) = 0;
  virtual void __fastcall Set_name(WideString Value) = 0;
  // Methods & Properties
  __property WideString address = { read=Get_address, write=Set_address };
  __property WideString name = { read=Get_name, write=Set_name };
};

// IXMLBodyType

__interface INTERFACE_UUID("{A67FC30A-D1E1-4893-AF86-37A4448D3B40}") IXMLBodyType : public IXMLNode
{
public:
  // Property Accessors
  virtual WideString __fastcall Get_text() = 0;
  virtual _di_IXMLattachmentList __fastcall Get_attachment() = 0;
  virtual void __fastcall Set_text(WideString Value) = 0;
  // Methods & Properties
  __property WideString text = { read=Get_text, write=Set_text };
  __property _di_IXMLattachmentList attachment = { read=Get_attachment };
};

// IXMLattachment

__interface INTERFACE_UUID("{8641C5D0-94A4-4FD8-970E-C6B3A1D42485}") IXMLattachment : public IXMLNode
{
public:
  // Property Accessors
  virtual WideString __fastcall Get_file() = 0;
  virtual WideString __fastcall Get_encoding() = 0;
  virtual void __fastcall Set_file(WideString Value) = 0;
  virtual void __fastcall Set_encoding(WideString Value) = 0;
  // Methods & Properties
  __property WideString file = { read=Get_file, write=Set_file };
  __property WideString encoding = { read=Get_encoding, write=Set_encoding };
};

// IXMLattachmentList

__interface INTERFACE_UUID("{8FCB9DF8-5A8F-4AA6-9047-3FEDEDEB9A90}") IXMLattachmentList : public IXMLNodeCollection
{
public:
  // Methods & Properties
  virtual _di_IXMLattachment __fastcall Add() = 0;
  virtual _di_IXMLattachment __fastcall Insert(const int Index) = 0;
  virtual _di_IXMLattachment __fastcall Get_Item(int Index) = 0;
  __property _di_IXMLattachment Items[int Index] = { read=Get_Item /* default */ };
};

// Forward Decls

class TXMLemailType;
class TXMLHeadType;
class TXMLnameAddressType;
class TXMLBodyType;
class TXMLattachment;
class TXMLattachmentList;

// TXMLemailType

class TXMLemailType : public TXMLNode, public IXMLemailType
{
  __IXMLNODE_IMPL__
protected:
  // IXMLemailType
  virtual _di_IXMLHeadType __fastcall Get_head();
  virtual _di_IXMLBodyType __fastcall Get_Body();
public:
  virtual void __fastcall AfterConstruction(void);
};


// TXMLHeadType

class TXMLHeadType : public TXMLNode, public IXMLHeadType
{
  __IXMLNODE_IMPL__
protected:
  // IXMLHeadType
  virtual _di_IXMLnameAddressType __fastcall Get_from();
  virtual _di_IXMLnameAddressType __fastcall Get_to();
  virtual WideString __fastcall Get_Subject();
  virtual void __fastcall Set_Subject(WideString Value);
public:
  virtual void __fastcall AfterConstruction(void);
};


// TXMLnameAddressType

class TXMLnameAddressType : public TXMLNode, public IXMLnameAddressType
{
  __IXMLNODE_IMPL__
protected:
  // IXMLnameAddressType
  virtual WideString __fastcall Get_address();
  virtual WideString __fastcall Get_name();
  virtual void __fastcall Set_address(WideString Value);
  virtual void __fastcall Set_name(WideString Value);
};


// TXMLBodyType

class TXMLBodyType : public TXMLNode, public IXMLBodyType
{
  __IXMLNODE_IMPL__
private:
  _di_IXMLattachmentList Fattachment;
protected:
  // IXMLBodyType
  virtual WideString __fastcall Get_text();
  virtual _di_IXMLattachmentList __fastcall Get_attachment();
  virtual void __fastcall Set_text(WideString Value);
public:
  virtual void __fastcall AfterConstruction(void);
};


// TXMLattachment

class TXMLattachment : public TXMLNode, public IXMLattachment
{
  __IXMLNODE_IMPL__
protected:
  // IXMLattachment
  virtual WideString __fastcall Get_file();
  virtual WideString __fastcall Get_encoding();
  virtual void __fastcall Set_file(WideString Value);
  virtual void __fastcall Set_encoding(WideString Value);
};


// TXMLattachmentList

class TXMLattachmentList : public TXMLNodeCollection, public IXMLattachmentList
{
  __IXMLNODECOLLECTION_IMPL__
protected:
  // IXMLattachmentList
  virtual _di_IXMLattachment __fastcall Add();
  virtual _di_IXMLattachment __fastcall Insert(const int Index);
  virtual _di_IXMLattachment __fastcall Get_Item(int Index);
};


// Global Functions

_di_IXMLemailType __fastcall Getemail(_di_IXMLDocument Doc);
_di_IXMLemailType __fastcall Getemail(TXMLDocument *Doc);
_di_IXMLemailType __fastcall Loademail(const WideString FileName);
_di_IXMLemailType __fastcall  Newemail();

#endif
#############################################################################
email.cpp

// ****************************************************** //
//                                                     
//                   XML Data Binding                  
//                                                     
//         Generated on: 2006-8-11 15:26:34            
//       Generated from: D:/My document/xml/email.xsd  
//   Settings stored in: D:/My document/xml/email.xdb  
//                                                     
// ****************************************************** //

#include <vcl.h>
#pragma hdrstop

#include "email.h"


// Global Functions

_di_IXMLemailType __fastcall Getemail(_di_IXMLDocument Doc)
{
  return (_di_IXMLemailType) Doc->GetDocBinding("email", __classid(TXMLemailType));
};

_di_IXMLemailType __fastcall Getemail(TXMLDocument *Doc)
{
  _di_IXMLDocument DocIntf;
  Doc->GetInterface(DocIntf);
  return Getemail(DocIntf);
};

_di_IXMLemailType __fastcall Loademail(const WideString FileName)
{
  return (_di_IXMLemailType) LoadXMLDocument(FileName)->GetDocBinding("email", __classid(TXMLemailType));
};

_di_IXMLemailType __fastcall  Newemail()
{
  return (_di_IXMLemailType) NewXMLDocument()->GetDocBinding("email", __classid(TXMLemailType));
};


// TXMLemailType

void __fastcall TXMLemailType::AfterConstruction(void)
{
  RegisterChildNode(WideString("head"), __classid(TXMLHeadType));
  RegisterChildNode(WideString("Body"), __classid(TXMLBodyType));
  TXMLNode::AfterConstruction();
};

_di_IXMLHeadType __fastcall TXMLemailType::Get_head()
{
  return (_di_IXMLHeadType) TXMLNode::ChildNodes->Nodes[WideString("head")];
};

_di_IXMLBodyType __fastcall TXMLemailType::Get_Body()
{
  return (_di_IXMLBodyType) TXMLNode::ChildNodes->Nodes[WideString("Body")];
};

// TXMLHeadType

void __fastcall TXMLHeadType::AfterConstruction(void)
{
  RegisterChildNode(WideString("from"), __classid(TXMLnameAddressType));
  RegisterChildNode(WideString("to"), __classid(TXMLnameAddressType));
  TXMLNode::AfterConstruction();
};

_di_IXMLnameAddressType __fastcall TXMLHeadType::Get_from()
{
  return (_di_IXMLnameAddressType) TXMLNode::ChildNodes->Nodes[WideString("from")];
};

_di_IXMLnameAddressType __fastcall TXMLHeadType::Get_to()
{
  return (_di_IXMLnameAddressType) TXMLNode::ChildNodes->Nodes[WideString("to")];
};

WideString __fastcall TXMLHeadType::Get_Subject()
{
  return TXMLNode::ChildNodes->Nodes[WideString("Subject")]->Text;
};

void __fastcall TXMLHeadType::Set_Subject(WideString Value)
{
  TXMLNode::ChildNodes->Nodes[WideString("Subject")]->NodeValue = Value;
};

// TXMLnameAddressType

WideString __fastcall TXMLnameAddressType::Get_address()
{
  return TXMLNode::AttributeNodes->Nodes[WideString("address")]->Text;
};

void __fastcall TXMLnameAddressType::Set_address(WideString Value)
{
  TXMLNode::SetAttribute(WideString("address"), Value);
};

WideString __fastcall TXMLnameAddressType::Get_name()
{
  return TXMLNode::AttributeNodes->Nodes[WideString("name")]->Text;
};

void __fastcall TXMLnameAddressType::Set_name(WideString Value)
{
  TXMLNode::SetAttribute(WideString("name"), Value);
};

// TXMLBodyType

void __fastcall TXMLBodyType::AfterConstruction(void)
{
  RegisterChildNode(WideString("attachment"), __classid(TXMLattachment));
  CollectionCreater<_di_IXMLattachmentList, TXMLattachmentList, IXMLattachment>::DoCreate(this, Fattachment, "attachment");
  TXMLNode::AfterConstruction();
};

WideString __fastcall TXMLBodyType::Get_text()
{
  return TXMLNode::ChildNodes->Nodes[WideString("text")]->Text;
};

void __fastcall TXMLBodyType::Set_text(WideString Value)
{
  TXMLNode::ChildNodes->Nodes[WideString("text")]->NodeValue = Value;
};

_di_IXMLattachmentList __fastcall TXMLBodyType::Get_attachment()
{
  return (_di_IXMLattachmentList) Fattachment;
};

// TXMLattachment

WideString __fastcall TXMLattachment::Get_file()
{
  return TXMLNode::AttributeNodes->Nodes[WideString("file")]->Text;
};

void __fastcall TXMLattachment::Set_file(WideString Value)
{
  TXMLNode::SetAttribute(WideString("file"), Value);
};

WideString __fastcall TXMLattachment::Get_encoding()
{
  return TXMLNode::AttributeNodes->Nodes[WideString("encoding")]->Text;
};

void __fastcall TXMLattachment::Set_encoding(WideString Value)
{
  TXMLNode::SetAttribute(WideString("encoding"), Value);
};

// TXMLattachmentList

_di_IXMLattachment __fastcall TXMLattachmentList::Add()
{
  return (_di_IXMLattachment) AddItem(-1);
};

_di_IXMLattachment __fastcall TXMLattachmentList::Insert(const int Index)
{
  return (_di_IXMLattachment) AddItem(Index);
};

_di_IXMLattachment __fastcall TXMLattachmentList::Get_Item(int Index)
{
  return (_di_IXMLattachment) TXMLNodeCollection::List->Nodes[Index];
};
下边是我们操作XML文件的时候的代码,这段代码生成一个描述电子邮件的XML文件。
    XMLDocument1->Version = "1.0";
    XMLDocument1->Encoding = "GB2312";
    try
    {
        _di_IXMLemailType email=Getemail(XMLDocument1);                         //首先得到根节点。这里是email节点
        email->head->from->Text="[email protected]";                              //发件人地址
        email->head->to->Text="[email protected]";                               //收件人地址
        email->head->Subject="测试电子邮件";                                    //电子邮件主题  

        email->Body->text="this is a test of the email!";                       //电子邮件内容
        email->Body->attachment->Add();                                         //增加一个附件 
        email->Body->attachment->Items[0]->Text="c://attachment.jpeg";          //附件的内容
        XMLDocument1->SaveToFile("email.xml");                                  //生成XML文件  
    }
    catch(...)
    {
        Application->MessageBox("error ocurred!", "Look", MB_OK);
    }

 

你可能感兴趣的:(C++,xml,properties,interface,encoding,methods)