9 #include "IException.h"
11 #include <nlohmann/json.hpp>
14 #include <QDomDocument>
15 #include <QDomElement>
18 using json = nlohmann::json;
35 QDomDocument doc(
"xmlInput");
38 if (!file.open(QIODevice::ReadOnly)) {
39 QString message = QString(
"Failed to open file for XML Input: [%1]").arg(xmlFile);
43 if (!doc.setContent(&file)) {
45 QString message = QString(
"Failed to use file for XML Input: [%1]").arg(xmlFile);
64 QDomElement docElem = doc.documentElement();
94 if (element.hasAttributes()) {
97 json attributeSection;
98 QDomNamedNodeMap attrMap = element.attributes();
99 for (
int i=0; i < attrMap.size(); i++) {
100 QDomAttr attr = attrMap.item(i).toAttr();
101 attributeSection[
"attrib_"+attr.name().toStdString()] = attr.value().toStdString();
105 if (!element.text().isEmpty()) {
106 attributeSection[
"_text"] = element.text().toStdString();
108 newJson[element.tagName().replace(
":",
"_").toStdString()] = attributeSection;
113 if (!element.text().isEmpty()) {
114 newJson[element.tagName().replace(
":",
"_").toStdString()] = element.text().toStdString();
118 newJson[element.tagName().replace(
":",
"_").toStdString()];
146 while (!element.isNull()) {
147 QDomElement next = element.firstChildElement();
151 if (!output.contains(element.tagName().toStdString())){
152 output.update(converted);
158 if (!output[element.tagName().toStdString()].is_array()) {
160 repeatedArray.push_back(output[element.tagName().toStdString()]);
161 output[element.tagName().replace(
":",
"_").toStdString()] = repeatedArray;
163 output[element.tagName().replace(
":",
"_").toStdString()].push_back(converted[element.tagName().toStdString()]);
172 if (output.contains(element.tagName().toStdString())) {
176 if (!output[element.tagName().toStdString()].is_array()) {
178 repeatedArray.push_back(output[element.tagName().toStdString()]);
179 output[element.tagName().replace(
":",
"_").toStdString()] = repeatedArray;
181 output[element.tagName().replace(
":",
"_").toStdString()].push_back(temporaryJson);
184 if (element.hasAttributes()) {
186 QDomNamedNodeMap attrMap = element.attributes();
187 for (
int j=0; j < attrMap.size(); j++) {
188 QDomAttr attr = attrMap.item(j).toAttr();
189 tempArea[
"attrib_"+attr.name().toStdString()] = attr.value().toStdString();
193 output[element.tagName().replace(
":",
"_").toStdString()] = tempArea;
196 output[element.tagName().toStdString()] =
197 convertXmlToJson(next, output[element.tagName().replace(
":",
"_").toStdString()]);
201 element = element.nextSiblingElement();