#include <iostream>
#include <vector>
#include "tinyxml2.h"
using namespace std;
using namespace tinyxml2;
struct LedInfo {
int CCD;
bool enable;
string title;
int chn;
};
int main() {
vector<LedInfo> ledList;
XMLDocument doc;
doc.LoadFile("example.xml");
XMLElement* root = doc.FirstChildElement("LedList");
if (root == nullptr) {
cout << "Error: no LedList element found in XML file." << endl;
return 1;
}
// Parse communication settings
XMLElement* commSet = root->FirstChildElement("CommSet");
if (commSet != nullptr) {
int commNum = commSet->IntAttribute("CommNum", 0);
int rate = commSet->IntAttribute("rate", 0);
int data = commSet->IntAttribute("data", 0);
int stopbit = commSet->IntAttribute("stopbit", 0);
const char* parityStr = commSet->Attribute("parity");
bool parity = (parityStr != nullptr) && (strcmp(parityStr, "Yes") == 0);
const char* flowStr = commSet->Attribute("flow");
bool flow = (flowStr != nullptr) && (strcmp(flowStr, "Yes") == 0);
// Do something with the communication settings...
cout << "Communication settings: CommNum=" << commNum << ", rate=" << rate
<< ", data=" << data << ", stopbit=" << stopbit << ", parity=" << parity
<< ", flow=" << flow << endl;
}
// Parse Led elements
for (XMLElement* led = root->FirstChildElement(); led != nullptr; led = led->NextSiblingElement()) {
if (strcmp(led->Name(), "CommSet") == 0) {
// Skip CommSet element, since we've already parsed it
continue;
}
LedInfo info;
info.CCD = led->IntAttribute("CCD", 0);
info.enable = led->FirstChildElement("enable")->BoolText();
info.title = led->FirstChildElement("title")->GetText();
info.chn = led->FirstChildElement("chn")->IntText();
ledList.push_back(info);
}
// Do something with the Led information
for (const auto& led : ledList) {
cout << "Led CCD=" << led.CCD << ", enable=" << led.enable << ", title=" << led.title
<< ", chn=" << led.chn << endl;
}
return 0;
}