g ++在linux下编译rapidxml 使用与过程中出现的问题解决
共四個文件需要引用
#include "rapidxml.hpp" #include "rapidxml_utils.hpp" #include "rapidxml_print.hpp" #include "rapidxml_iterators.hpp"?1:修改rapidxml_iterators.hpp文件? 第20 和 102 行
? ? ? ? // typedef typename xml_node<Ch> value_type;
? ? ? ? // typedef typename xml_node<Ch> &reference;
? ? ? ? // typedef typename xml_node<Ch> *pointer;
? ? ? ? // typedef std::ptrdiff_t difference_type;
? ? ? ? // typedef std::bidirectional_iterator_tag iterator_category;
為下面的內容
? ? ? ? typedef xml_node<Ch> value_type;
? ? ? ? typedef xml_node<Ch> &reference;
? ? ? ? typedef xml_node<Ch> *pointer;
? ? ? ? typedef typename std::ptrdiff_t difference_type;
? ? ? ? typedef typename std::bidirectional_iterator_tag iterator_category;
? ? ? ?// typedef typename xml_attribute<Ch> value_type;
? ? ? ? // typedef typename xml_attribute<Ch> &reference;
? ? ? ? // typedef typename xml_attribute<Ch> *pointer;
? ? ? ? // typedef std::ptrdiff_t difference_type;
? ? ? ? // typedef std::bidirectional_iterator_tag iterator_category;
為下面的內容
? ? ? ? typedef xml_node<Ch> value_type;
? ? ? ? typedef xml_node<Ch> &reference;
? ? ? ? typedef xml_node<Ch> *pointer;
? ? ? ? typedef typename std::ptrdiff_t difference_type;
? ? ? ? typedef typename std::bidirectional_iterator_tag iterator_category;
2:rapidxml_print.hpp? 文件? ?在文件的:124行處插入如下代碼
template<class OutIt, class Ch> inline OutIt print_children(OutIt out, const xml_node<Ch>* node, int flags, int indent);template<class OutIt, class Ch> inline OutIt print_attributes(OutIt out, const xml_node<Ch>* node, int flags);template<class OutIt, class Ch> inline OutIt print_data_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);template<class OutIt, class Ch> inline OutIt print_cdata_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);template<class OutIt, class Ch> inline OutIt print_element_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);template<class OutIt, class Ch> inline OutIt print_declaration_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);template<class OutIt, class Ch> inline OutIt print_comment_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);template<class OutIt, class Ch> inline OutIt print_doctype_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);template<class OutIt, class Ch> inline OutIt print_pi_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);?
更改完畢即可使用了
1.寫文件
#include <iostream> #include "../rapidxml/rapidxml.hpp" #include "../rapidxml/rapidxml_utils.hpp" #include "../rapidxml/rapidxml_print.hpp" #include "../rapidxml/rapidxml_iterators.hpp"using namespace rapidxml;int main() { xml_document<> doc; xml_node<>* rot = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='utf-8'"));doc.append_node(rot);xml_node<>* node = doc.allocate_node(node_element,"config","information"); xml_node<>* color = doc.allocate_node(node_element,"color",NULL); //建議使用如下方法,否則臨時變量的指針出了作用域,容易造成xml節點問題//std::string s = "color";//char* pname = doc.allocate_string(s.c_str());//doc.allocate_node(node_element,pname,NULL)doc.append_node(node);node->append_node(color);color->append_node(doc.allocate_node(node_element,"red","0.1"));color->append_node(doc.allocate_node(node_element,"green","0.1"));color->append_node(doc.allocate_node(node_element,"blue","0.1"));color->append_node(doc.allocate_node(node_element,"alpha","1.0"));xml_node<>* size = doc.allocate_node(node_element,"size",NULL); size->append_node(doc.allocate_node(node_element,"x","640"));size->append_node(doc.allocate_node(node_element,"y","480"));node->append_node(size);xml_node<>* mode = doc.allocate_node(rapidxml::node_element,"mode","screen mode");mode->append_attribute(doc.allocate_attribute("fullscreen","false"));node->append_node(mode);std::string text; rapidxml::print(std::back_inserter(text), doc, 0); std::cout<<text<<std::endl; std::ofstream out("config.xml");out << doc;system("PAUSE");return EXIT_SUCCESS; }2.讀文件 :
#include <iostream> #include "../rapidxml/rapidxml.hpp" #include "../rapidxml/rapidxml_utils.hpp" #include "../rapidxml/rapidxml_print.hpp" #include "../rapidxml/rapidxml_iterators.hpp" using namespace rapidxml;int main() {file<> fdoc("config.xml");std::cout << fdoc.data() << std::endl;xml_document<> doc;doc.parse<parse_full>(fdoc.data());std::cout << doc.name() << std::endl;//! 獲取根節點xml_node<> *root = doc.first_node("config");std::cout << root->name() << std::endl;//! 獲取根節點第一個節點xml_node<> *node1 = root->first_node();std::cout << node1->name() << std::endl;xml_node<> *node11 = node1->first_node();std::cout << node11->name() << std::endl;std::cout << node11->value() << std::endl;//! 修改之后再次保存xml_node<> *size = root->first_node("size");// rapidxml::xml_attribute<>* nameAttr = child->first_attribute("name");size->append_node(doc.allocate_node(node_element, "w", "0"));size->append_node(doc.allocate_node(node_element, "h", "0"));std::string text;rapidxml::print(std::back_inserter(text), doc, 0);std::cout << text << std::endl;std::ofstream out("config.xml");out << doc;system("PAUSE");return EXIT_SUCCESS; }運行結果?config.xml
<?xml version="1.0" encoding="utf-8"?>
<config>
?? ?<color>
?? ??? ?<red>0.1</red>
?? ??? ?<green>0.1</green>
?? ??? ?<blue>0.1</blue>
?? ??? ?<alpha>1.0</alpha>
?? ?</color>
?? ?<size>
?? ??? ?<x>640</x>
?? ??? ?<y>480</y>
?? ??? ?<w>0</w>
?? ??? ?<h>0</h>? ? ?
?? ?</size>
?? ?<mode fullscreen="false">screen mode</mode>
</config>
3.讀xml 格式數據
#include <iostream> #include "../rapidxml/rapidxml.hpp" #include "../rapidxml/rapidxml_utils.hpp" #include "../rapidxml/rapidxml_print.hpp" #include "../rapidxml/rapidxml_iterators.hpp"using namespace rapidxml;void readFile(char *strc) {rapidxml::xml_document<> doc;doc.parse<0>(strc);//獲取根節點 rapidxml::xml_node<> *root = doc.first_node("Package");int nSize = 0;std::cout << "config=" << root->name() << std::endl;// rapidxml::xml_node<> *child = root->first_node("LtdInfo")->first_node(); //找到 LtdInfo 節點// std::cout << "LtdInfo=" << child->name() << std::endl;// std::cout << "LtdInfo=" << child->value() << std::endl;for (rapidxml::xml_node<> *child = root->first_node("LtdInfo")->first_node(); child; child = child->next_sibling()){std::cout << "" << child->name() << ":=" << child->value() << std::endl;}for (rapidxml::xml_node<> *child = root->first_node("Data")->first_node(); child; child = child->next_sibling()){std::cout << "" << child->name() << ":=" << child->value() << std::endl;}strc = NULL; }int main() { char strbuf[] = "<Package>""<LtdInfo>""<Code>[41574100001454]</Code>""<Pwd>[111111]</Pwd>""<Class>[N010]</Class>""<Flag>[20220408210546445]</Flag>""</LtdInfo>""<Data>""<Name>[Datatype,DateTime,millisecond,Data]</Name>""<Value>[0,2022-04-08 21:05:46,445,21.6,20.3,18.6,17.6,16.7,16.6,15.6,14.4,14.2,14.2,12.5,10.6,8.8,9.3,8.2,6.3,7.1,4.6,4.9,3.9,0.1,11.8,1.4,-0.8,-1.7,-0.7,13.8,-1.5,-2.6,-0.8,-4.3,-2.1,-2.5,1.9]</Value>""</Data>""</Package>";readFile(strbuf); }總結
以上是生活随笔為你收集整理的g ++在linux下编译rapidxml 使用与过程中出现的问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: unix编程艺术中的17点编程原则--设
- 下一篇: java移位运算符详解_java移位运算