8.4 xml编码与解码包

来源:互联网 发布:淘宝会员名怎么改不了 编辑:程序博客网 时间:2024/05/20 20:18

http://www.cppblog.com/firectrl/archive/2008/04/20/47639.html

http://www.cppblog.com/janvy/archive/2010/03/25/110496.aspx

tinyxml  是一个专门用于C++的XMl文件的编码与解码的类,

class TiXmlDocument;//文档类
class TiXmlElement; //主要的部分,成员类
class TiXmlComment;//注释类
class TiXmlUnknown;//
class TiXmlAttribute;//属性类,某个成员的属性
class TiXmlText;// 文本类
class TiXmlDeclaration;//版本和头的声明类
class TiXmlParsingData;//解析类

int messageCliDllSample::sendMessageRes(std::string phone_no,std::string message_text,int res)
{
msgResultReceiverSerivceSoap11BindingProxy messageCli;
messageCli.soap_endpoint = srvAddr_.c_str();


_ns1__waitReceivePhoneMsgResult _ns1__waitReceivePhoneMsgResult;
__ns2__waitReceivePhoneMsgResultResponse __ns2__waitReceivePhoneMsgResultResponse;
//
TiXmlDeclaration * xmlDec = new TiXmlDeclaration("1.0", "UTF-8", "yes");  
        TiXmlDocument * xmlDocs = new TiXmlDocument();
        xmlDocs->LinkEndChild(xmlDec); 


        TiXmlElement * results = new TiXmlElement("results"); 
        xmlDocs->LinkEndChild(results);  
TiXmlComment * comment = new TiXmlComment(" This is a list of message results ");  
results->LinkEndChild(comment);

TiXmlElement * result = new TiXmlElement("result");   
results->LinkEndChild(result); 

TiXmlElement * phone = new TiXmlElement("phone"); 
result->LinkEndChild(phone);
TiXmlText *phoneContent = new TiXmlText(phone_no.c_str());
phone->LinkEndChild(phoneContent);

TiXmlElement * status = new TiXmlElement("status"); 
result->LinkEndChild(status);
char strTmp[16] = {0};
sprintf(strTmp,"%d",res);
TiXmlText *statusContent = new TiXmlText(strTmp);
status->LinkEndChild(statusContent);

TiXmlElement * desc = new TiXmlElement("desc"); 
result->LinkEndChild(desc);
TiXmlText *descContent = new TiXmlText(message_text.c_str());
desc->LinkEndChild(descContent);

TiXmlPrinter printer; 
xmlDocs->Accept(&printer); 


std::string strRes= printer.Str();
int len = strRes.length();
std::cout<<strRes<<std::endl;

strRes = Base64_encode(strRes);

_ns1__waitReceivePhoneMsgResult.sendResult = &strRes;

messageCli.waitReceivePhoneMsgResult(&_ns1__waitReceivePhoneMsgResult,__ns2__waitReceivePhoneMsgResultResponse);

if (messageCli.error)
messageCli.soap_stream_fault(std::cerr);
else
printf("send message_text : %s\n",message_text.c_str());

delete [] xmlDocs; //最后要将作为容器的文档类删除
return 0;
}

///////////////////////////////////////////////////////////////////////////////调用属性类:

    TiXmlDeclaration * xmlDec = new TiXmlDeclaration("1.0", "UTF-8", "yes");  
    TiXmlDocument * xmlDocs = new TiXmlDocument();
    xmlDocs->LinkEndChild(xmlDec); 

    TiXmlElement * meters = new TiXmlElement("meters"); 
    xmlDocs->LinkEndChild(meters);  
TiXmlComment * comment = new TiXmlComment(" This is a list of meters rand data");  
meters->LinkEndChild(comment);

//data
std::map<unsigned int,unsigned int>::iterator pn_map_data_it_;
for(pn_map_data_it_ = pn_map_data.begin(); pn_map_data_it_ != pn_map_data.end(); pn_map_data_it_++)
{
TiXmlElement * meter = new TiXmlElement("meter");  
meter->SetAttribute("id", ""); //设置属性
meter->SetAttribute("pn", pn_map_data_it_->second);  
meter->SetAttribute("termlogic", strLogicRes);  
meters->LinkEndChild(meter); 


std::map<struct pn_fn_key,std::deque<struct FEP_RAND_DATA>> ::iterator map_data_it_;
for(map_data_it_ = map_data.begin(); map_data_it_ != map_data.end(); map_data_it_++)
{
if(map_data_it_->first.pn != pn_map_data_it_->second)//meter
continue;


TiXmlElement * datas = new TiXmlElement("datas"); 
datas->SetAttribute("fn",map_data_it_->first.fn); 
meter->LinkEndChild(datas);


for(int i=0; i<map_data_it_->second.size(); i++)
{
char str_tv[64] = {0};
time_t tv_t = map_data_it_->second[i].times;
std::tm cur_tm = *localtime(&tv_t);
sprintf(str_tv,"%04d-%02d-%02d %02d:%02d:%02d",cur_tm.tm_year+1900,cur_tm.tm_mon+1,cur_tm.tm_mday,\
cur_tm.tm_hour,cur_tm.tm_min,cur_tm.tm_sec);


char valStr[16] = {0};
sprintf(valStr,"%.3f",map_data_it_->second[i].values);


TiXmlElement * data = new TiXmlElement("data"); 
data->SetAttribute("code", map_data_it_->second[i].datatype);
data->SetAttribute("desc", map_data_it_->second[i].desc);
data->SetAttribute("value", valStr);
data->SetAttribute("tv", str_tv);
data->SetAttribute("validity",  map_data_it_->second[i].validity);
data->SetAttribute("unit", map_data_it_->second[i].unit);
datas->LinkEndChild(data); 
}
}
}

原创粉丝点击