C++中实现对象的SOAP封装

SOAP封装本质上是一种特殊的XML封装,在.NET中使用XmlTextWriter可以很方便的构造XML文本流。

所以,本文首先实现SoapWriter类,然后再提供对象的SOAP封装范例。

class SoapWriter { public: SoapWriter(size_t nTabOffset = 0); virtual ~PMESoapWriter(); public: // 以下函数中的name为空时,将使用类型名称作为元素名 void WriteBool(SoapBoolean value, const char* name = 0, const char* xmlns = 0); void WriteChar(SoapChar value, const char* name = 0, const char* xmlns = 0); void WriteShort(SoapShort value, const char* name = 0, const char* xmlns = 0); void WriteInt32(SoapInt32 value, const char* name = 0, const char* xmlns = 0); void WriteInt64(SoapInt64 value, const char* name = 0, const char* xmlns = 0); void WriteFloat(SoapFloat value, const char* name = 0, const char* xmlns = 0); void WriteDouble(SoapDouble value, const char* name = 0, const char* xmlns = 0); void WriteString(SoapString value, const char* name = 0, const char* xmlns = 0); void WriteWString(SoapWString value, const char* name = 0, const char* xmlns = 0); public: void WriteHeader(); public: void StartStruct(const char* name, const char* xmlns = 0); // 开始定义结构 void EndStruct(); // 结束定义结构 void StartArray(const char* name, const char* xmlns = 0); // 开始定义数组 void EndArray(); // 结束定义数组 public: void ResetContent(); // 清除所有状态数据和SOAP文本 const char* ToString(); size_t GetLength(); };

看一个Point结构的Soap封装过程:

const char* GetSoapText(Point pt) { SoapWriter writer; writer.WriteHeader(); writer.StartStruct("Point"); writer.WriteInt32(pt.x, "x"); writer.WriteInt32(pt.y, "y"); writer.EndStruct(); } // 输出结果 // // // 10 // 20 //

再看一个int数组的Soap封装过程:

const char* GetSoapText(int values[], int length) { SoapWriter writer; writer.WriteHeader(); writer.StartArray("values"); for (int n = 0; n < length; ++n) { writer.WriteInt32(values[n]); } writer.EndArray(); } // 输出结果 // // // 10 // 20 // 30 //

有兴趣的朋友可以来邮索取SoapWriter的源代码……广结扣友(扣友:喜欢Coding的朋友)

[email protected]

你可能感兴趣的:(软件分析与设计)