#include <stdio.h>
#include "tinyxml2.h"
using namespace tinyxml2;
int main()
{
XMLDocument doc;
if(doc.LoadFile("DC.xml")!=0)
{
printf("load DC.xml err");
return -1;
}
printf("load DC.xml success\n");
XMLElement *root = doc.RootElement();
XMLElement *host = root->FirstChildElement("host");
if(host == NULL)
{
printf("load DC.xml err");
return -1;
}
const char* strip = host->Attribute("ip");
int port = host->IntAttribute("port");
printf("host ip = %s ,port = %d\n" ,strip,port);
XMLElement* plugins = root->FirstChildElement("plugins");
if (NULL != plugins)
{
XMLElement* plugin = plugins->FirstChildElement("plugin");
for (;;)
{
if(plugin == NULL)
{
break;
}
const char* collect_type = plugin->Attribute("collect_type");
const char* name = plugin->Attribute("name");
printf("plugin collect_type = %s ,name = %s\n" ,collect_type,name);
plugin = plugin->NextSiblingElement();
}
}
else
{
printf("load DC.xml err");
return -1;
}
return 0;
}
<?xml version="1.0" encoding="utf-8" ?>
<config>
<host ip="127.0.0.1" port="7500" />
<plugins>
<plugin collect_type="30010" name="dc_1" />
<plugin collect_type="30020" name="dc_2" />
</plugins>
</config>