Tinyxml 示例
int add(const char* ip,const char* name)
{
TiXmlDocument vmmdoc("vmmconfig.xml");
TiXmlNode *phy;
bool loadOkay = vmmdoc.LoadFile();
if ( !loadOkay )
{
printf( "Could not load test file 'vmmconfig.xml'. Error='%s'. Exiting.\n", vmmdoc.ErrorDesc() );
exit( 1 );
}
phy=vmmdoc.RootElement()->FirstChild("physics_hosts");
TiXmlElement host("host");
TiXmlElement ipel("ip");
TiXmlElement nameel("name");
TiXmlText *ipText = new TiXmlText(ip);
TiXmlText *nameText = new TiXmlText(name);
ipel.LinkEndChild(ipText);
nameel.LinkEndChild(nameText);
host.InsertEndChild(ipel);
host.InsertEndChild(nameel);
phy->InsertEndChild(host);
vmmdoc.SaveFile();
return 1;
}
int addToXMLFile(const char els[][30], const char values[][30], int length) {
TiXmlDocument vmmdoc("spu.xml");
bool loadOkay = vmmdoc.LoadFile();
if (!loadOkay) {
printf(
"Could not load test file 'vmmconfig.xml'. Error='%s'. Exiting.\n",
vmmdoc.ErrorDesc());
exit(1);
}
TiXmlNode *root = vmmdoc.RootElement()->FirstChild("physics_spus");
TiXmlElement *title = new TiXmlElement("spu");
for (int i = 0; i < length; i++) {
TiXmlElement *el = new TiXmlElement(els[i]);
TiXmlText *tx = new TiXmlText(values[i]);
el->LinkEndChild(tx);
title->InsertEndChild(*el);
delete el;
}
root->InsertEndChild(*title);
vmmdoc.SaveFile();
delete title;
return 1;
}
用法:
const char els[5][30]={"id","lsn","psn","mem","loadservice"};
const char values[5][30]={"1","2","3","512000","xorp"};
cout<<addToXMLFile(els,values,5);
躲猫猫社团团长 http://t.sina.com.cn/coolria