tinyxml使用

来源:互联网 发布:淘宝怎么完成实名认证 编辑:程序博客网 时间:2024/05/20 10:21

       创建一个xml文档 

       TiXmlDocument doc;

TiXmlDeclaration *decl=new TiXmlDeclaration("1.0","GB2312","yes");
doc.LinkEndChild(decl);
TiXmlElement *request=new TiXmlElement("request");
request->SetAttribute("command","CuRegister");
doc.LinkEndChild(request);


TiXmlElement *Paras=new TiXmlElement("parameters"); 
request->LinkEndChild(Paras);


TiXmlElement *Para=new TiXmlElement("cuPassword");
MD5 md5("12345");
std::string result = md5.md5();
Para->LinkEndChild( new TiXmlText( result.c_str())); 
Paras->LinkEndChild(Para);


Para=new TiXmlElement("cuUserId");
Para->LinkEndChild( new TiXmlText( "css" )); 
Paras->LinkEndChild(Para);


Para=new TiXmlElement("cuIp");
Para->LinkEndChild( new TiXmlText( "172.168.10.10" )); 
Paras->LinkEndChild(Para);


Para=new TiXmlElement("cuPort");
Para->LinkEndChild( new TiXmlText( "9001" )); 
Paras->LinkEndChild(Para);


Para=new TiXmlElement("version");
Para->LinkEndChild( new TiXmlText( "01" )); 
Paras->LinkEndChild(Para);
     
TiXmlPrinter printer;


printer.SetIndent( "\t" );
doc.Accept( &printer );
TiXmlDocument doc2;
doc2.Parse(printer.CStr());
doc2.SaveFile("a.xml"); //可以保存成xml文件
//char *p=printer.CStr()

CuRegisterParse(printer.CStr());  //将doc付给TiXmlPrinter可以将xml转变成字符串形式


xml解析



void  CuRegisterParse(const char* str)

{

TiXmlDocument doc;
doc.Parse(str);    //通过Parse将字符串形式的xml转换成DOM形态
TiXmlElement* root = doc.FirstChildElement( "request" );
if ( root )
{
TiXmlAttribute* pAttrib=root->FirstAttribute();
cout<<pAttrib->Value()<<endl;
TiXmlElement* element = root->FirstChildElement( "parameters" );
if ( element )
{
TiXmlElement* childelement=element->FirstChildElement( "cuPassword" );
cout<<childelement->GetText()<<endl;
if(childelement)
{
TiXmlElement* child2 = childelement->NextSiblingElement( "cuUserId" );
if(child2)
cout<<child2->GetText()<<endl;
}

}

}
}

0 0