RapidXml使用小结
还是那句话,代码使用的粗陋,但是可以参考:(注意变量生命周期)
void saveAsXml(const CString& path) { if (m_pRoot) { //::DeleteFile(path); //using namespace rapidxml; //xml_document<> doc; root->remove_all_nodes(); root->remove_all_attributes(); m_pRoot->SaveConfigureAsXml(doc, root); std::string xml_as_string; rapidxml::print(std::back_inserter(xml_as_string), doc); std::ofstream fileStored("file.xml"); fileStored << xml_as_string; fileStored.close(); //doc.clear(); /*CFile file; file.Open(path, CFile::modeCreate | CFile::modeReadWrite); file.Write(pBuffer, Used); file.Close();*/ } } void loadFromXml(const CString& path, int xmlVersion/* = 0*/) { //xml_node<> * root_node; const auto xmlFile = loadFile(path);//"file_stored.xml" if (!xmlFile.empty()) { const auto tmp = xmlFile; processXmlFile(xmlFile.data(), xmlVersion); } } void processXmlFile(const char* data, int xmlVersion) { enum { PARSE_FLAGS = rapidxml::parse_non_destructive }; // NOTE : There is a `const_cast<>`, but `rapidxml::parse_non_destructive` // guarantees `data` is not overwritten. rapidxml::xml_document<> xmlDoc; xmlDoc.parse<PARSE_FLAGS>(const_cast<char*>(data));//root_node = doc.first_node("root")->first_node("XXXRectangle"); //walk(doc.first_node(),0,xmlVersion);//doc.first_node() xml_node<>* node = xmlDoc.first_node(); std::vector<const xml_node<>*> vec0; vec0.push_back(node); std::vector<std::vector<const xml_node<>*>> vecAll; vecAll.push_back(vec0); for (int indent = 0;; ++indent){ if (indent>=vecAll.size()) { break; } std::vector<const xml_node<>*>& vec=vecAll[indent]; std::vector<const xml_node<>*> add; for (int i = 0; i < vec.size();++i) { node = const_cast<xml_node<>*>(vec[i]); const auto ind = std::string(indent * 4, ' '); printf("%s", ind.c_str()); const node_type t = node->type(); switch (t) { case node_element: { if (xmlVersion == 2) { if (!strncmp(node->name(), "mxCell", 6)) { xml_attribute<>* a = node->first_attribute("id"),*b=NULL; string id(a->value(), a->value_size()); if (std::stoi(id) != 0) { //if (std::stoi(id) == 1) { //} else { a = node->first_attribute("style"); b = node->first_attribute("value"); if (a != NULL) { string* style = new string(a->value(), a->value_size()); if (b != NULL && b->value_size()>0) { string value(b->value(), b->value_size()); size_t start = value.find(""><b>"); if (start != string::npos) { size_t end = value.find("<", start + 19); if (end != string::npos) { LoadXXXText2(const_cast<xml_node<>*>(node), xmlVersion); }else { LoadXXXRectangle2(const_cast<xml_node<>*>(node), xmlVersion); } }else { start = value.find("">"); if (start != string::npos) { LoadXXXText2(const_cast<xml_node<>*>(node), xmlVersion); }else { LoadXXXRectangle2(const_cast<xml_node<>*>(node), xmlVersion); } } } else if (style->find("。。。。") != string::npos || style->find("。。。。")!=string::npos || style->find("。。。。") != string::npos) { LoadXXXStaticDrawing2(const_cast<xml_node<>*>(node), xmlVersion);//圆 }else if (style->find("whiteSpace") != string::npos) { LoadXXXRectangle2(const_cast<xml_node<>*>(node), xmlVersion); }else if (style->find("startArrow") != string::npos) {//折线 LoadXXXStaticDrawing2(const_cast<xml_node<>*>(node), xmlVersion); }else if (style->find("endArrow") != string::npos) {//线段 LoadXXXLine2(const_cast<xml_node<>*>(node), xmlVersion); } delete style; } //} } } } else { //printf("<%.*s", node->name_size(), node->name()); if (!strncmp(node->name(), "XXXBase", 11)) { LoadXXXBase2(const_cast<xml_node<>*>(node), xmlVersion); } else if (!strncmp(node->name(), "XXXRectangle", 16)) { if (strncmp(node->first_attribute("Parent")->value(), "-1", 2)) { LoadXXXRectangle2(const_cast<xml_node<>*>(node), xmlVersion); } } else if (!strncmp(node->name(), "XXXRadioButton", 18)) { LoadXXXRadioButton2(const_cast<xml_node<>*>(node), xmlVersion); } else if (!strncmp(node->name(), "XXXText", 11)) { LoadXXXText2(const_cast<xml_node<>*>(node), xmlVersion); } else if (!strncmp(node->name(), "XXXLine", 11)) { LoadXXXLine2(const_cast<xml_node<>*>(node), xmlVersion); } else if (!strncmp(node->name(), "XXXStaticDrawing", 20)) { LoadXXXStaticDrawing2(const_cast<xml_node<>*>(node), xmlVersion); } } //for (const xml_attribute<>* a = node->first_attribute(); a; a = a->next_attribute()) { // printf(" %.*s", a->name_size(), a->name()); // printf("='%.*s'", a->value_size(), a->value()); //} //printf(">\n"); for (const xml_node<>* n = node->first_node(); n; n = n->next_sibling()) { add.push_back(n); //walk(n, indent + 1, xmlVersion); } //printf("%s</%.*s>\n", ind.c_str(), node->name_size(), node->name()); } break; case node_data: printf("DATA:[%.*s]\n", node->value_size(), node->value()); break; default: printf("NODE-TYPE:%d\n", t); break; } } if (add.size() > 0) { vecAll.push_back(add); } } } void init(xml_node<>* node, XXXBase* m_pRoot, xml_document<>& doc, int xmlVersion) { data.FatherId = GetIntValueByName(node, "Parent", -1, xmlVersion); ... data.ResetEcho = GetStrValueByName(node, "ResetEcho", "reset", xmlVersion); //============ data.D90 = GetIntValueByName(node, "D90", 0, xmlVersion); ... data.Ziti = GetStrValueByName(node, "Ziti", "黑体", xmlVersion); if (true || node == NULL || xmlVersion == 2) { this->m_node = doc.allocate_node(node_element, "XXXRectangle"); this->m_node->append_attribute(doc.allocate_attribute("Parent", doc.allocate_string(std::to_string(data.FatherId).c_str()))); this->m_node->append_attribute(doc.allocate_attribute("ID", doc.allocate_string(std::to_string(data.ID).c_str()))); this->m_node->append_attribute(doc.allocate_attribute("NameofIt", doc.allocate_string(data.NameofIt.c_str()))); ... this->m_node->append_attribute(doc.allocate_attribute("BitCheckedValueFrm", doc.allocate_string(std::to_string(data.BitCheckedValueFrm).c_str()))); this->m_node->append_attribute(doc.allocate_attribute("BitCheckedValueToo", doc.allocate_string(std::to_string(data.BitCheckedValueToo).c_str()))); ... //=== this->m_node->append_attribute(doc.allocate_attribute("D90", doc.allocate_string(std::to_string(data.D90).c_str()))); ... } XXXBase* pParent = GetWidgetById(m_pRoot, data.FatherId); pParent->AddChild(this); this->WidgetId = data.ID; if (theApp.GlobalIndex <= data.ID) theApp.GlobalIndex = data.ID + 1; ANSIToUnicode(data.NameofIt.c_str(), this->NameofItPlus); this->LcX = data.LcX; ... this->pFramePen = new Pen(Color(data.pFramePenColorA, data.pFramePenColorR, data.pFramePenColorG, data.pFramePenColorB), data.PenWidth); this->dashVals[0] = data.dashVals[0]; this->dashVals[1] = data.dashVals[1]; if (this->dashVals[0]>0. && this->dashVals[1]>0.) { this->pFramePen->SetDashPattern(this->dashVals, 2); } this->pMainBrush = new SolidBrush(Color(data.pMainBrushColorA, data.pMainBrushColorR, data.pMainBrushColorG, data.pMainBrushColorB)); this->m_pAboutReg = FindRegObject(strtoul(data.m_ulBaseAddress.c_str(), NULL, 16), strtoul(data.m_ulOffsetAddress.c_str(), NULL, 16)); if (this->m_pAboutReg) { AddRegToTable(this->m_pAboutReg); } this->m_pFuncSetEnable = (FUN_SET_ENABLE)FindFunction(CString(data.m_pFuncSetEnable.c_str())); ... //=== this->D90 = data.D90; this->DrawBk = data.DrawBk; ... this->TextSize = data.TextSize; ANSIToUnicode(data.ShowText.c_str(), this->ShowText); this->pTextBrush = new SolidBrush(Color(data.pTextBrushColorA, data.pTextBrushColorR, data.pTextBrushColorG, data.pTextBrushColorB)); ANSIToUnicode(data.Ziti.c_str(), this->Ziti); } BOOL SaveConfigureAsXml(xml_document<>& doc, xml_node<> * parent) { XXXBase* pEachSon = pFirstSun; while (NULL != pEachSon) { pEachSon->SaveConfigureAsXml(doc, m_node); pEachSon = pEachSon->pBrother; } if (theApp.GlobalIndex <= data.ID) theApp.GlobalIndex = data.ID + 1; //== char rsut[128]; UnicodeToANSI(this->NameofItPlus, rsut); this->m_node->first_attribute("NameofIt")->value(doc.allocate_string(rsut)); ... Color c; this->pFramePen->GetColor(&c); this->m_node->first_attribute("pFramePenColorA")->value(doc.allocate_string(std::to_string(static_cast<int>(c.GetA())).c_str())); ... this->pMainBrush->GetColor(&c); this->m_node->first_attribute("pMainBrushColorA")->value(doc.allocate_string(std::to_string(static_cast<int>(c.GetA())).c_str())); ... if (m_pAboutReg) { CString FloatString; FloatString.Format("0x%08X", m_pAboutReg->m_ulBaseAddress); this->m_node->first_attribute("m_ulBaseAddress")->value(doc.allocate_string(FloatString));//mark FloatString.Format("0x%04X", m_pAboutReg->m_ulOffsetAddress); this->m_node->first_attribute("m_ulOffsetAddress")->value(doc.allocate_string(FloatString)); } this->m_node->first_attribute("m_pFuncSetEnable")->value(doc.allocate_string(GetFuncNameByAddr(this->m_pFuncSetEnable))); ... UnicodeToANSI(this->ResetEcho, rsut); this->m_node->first_attribute("ResetEcho")->value(doc.allocate_string(rsut)); //=== if (this->TextSize > 0) { this->m_node->first_attribute("D90")->value(doc.allocate_string(std::to_string(this->D90).c_str())); this->m_node->first_attribute("DrawBk")->value(doc.allocate_string(std::to_string(this->DrawBk).c_str())); .... UnicodeToANSI(this->ShowText, rsut); this->m_node->first_attribute("ShowText")->value(doc.allocate_string(rsut)); } if (this->pTextBrush) { this->pTextBrush->GetColor(&c); this->m_node->first_attribute("pTextBrushColorA")->value(doc.allocate_string(std::to_string(c.GetA()).c_str())); .... UnicodeToANSI(this->Ziti, rsut); this->m_node->first_attribute("Ziti")->value(doc.allocate_string(rsut)); } parent->append_node(m_node); return TRUE; }
参考:https://www.cnblogs.com/hihilary/archive/2012/11/18/2772983.html
参考:https://www.cnblogs.com/chutianyao/p/3246592.html
参考:https://blog.csdn.net/wqvbjhc/article/details/7662931
参考:https://github.com/dwd/rapidxml
参考:https://github.com/dwd/rapidxml/blob/master/test/simple.cpp
参考:https://blog.csdn.net/u012209790/article/details/56014779
参考:https://stackoverflow.com/questions/4583409/add-number-double-float-as-attribute-to-rapidxml-node
参考:http://rapidxml.sourceforge.net/manual.html#namespacerapidxml_1printing
参考:https://stackoverflow.com/questions/46300376/rapidxml-parsing-misbehavior-when-subfunction-is-used
参考:https://github.com/rstudio/rstudio/blob/master/src/cpp/core/include/core/rapidxml/rapidxml.hpp
参考:https://stackoverflow.com/questions/53593933/write-to-xml-with-rapidxml
参考:https://gist.github.com/JSchaenzle/2726944
参考:https://blog.csdn.net/hellokandy/article/details/56834236
参考:https://gist.github.com/t-mat/7150417
参考:https://gist.github.com/JSchaenzle/2726944
参考:https://stackoverflow.com/questions/2808022/how-to-parse-the-xml-file-in-rapidxml
![](https://res.hc-cdn.com/ecology/9.3.130/v2_resources\images\global\blog-dialog.png)
华为开发者空间发布
让每位开发者拥有一台云主机
- 点赞
- 收藏
- 关注作者
评论(0)