g ++在linux下编译rapidxml 使用与过程中出现的问题解决[通俗易懂]

g ++在linux下编译rapidxml 使用与过程中出现的问题解决[通俗易懂]rapidxmlunderlinuxwithg++

大家好,又见面了,我是你们的朋友全栈君。

共四个文件需要引用

#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);

g ++在linux下编译rapidxml 使用与过程中出现的问题解决[通俗易懂]

 

更改完毕即可使用了

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 使用与过程中出现的问题解决[通俗易懂]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160907.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 电脑windows是什么文件夹_windows10的目录结构

    电脑windows是什么文件夹_windows10的目录结构windows文件介绍总结├WINDOWS│├-system32(存放Windows的系统文件和硬件驱动程序)││├-config(用户配置信息和密码信息)│││└-system

    2022年8月2日
    5
  • 如何系统备份ghost_服务器可以用pe备份吗

    如何系统备份ghost_服务器可以用pe备份吗电脑出现系统故障是一个很正常的现象,在这个时候只能通过重组系统的方法来解决故障,如果我们此前有将正常的系统备份到U盘里面那么重装系统就会变得很简单,接下来就教给大家怎样用GHOST备份系统。1、首先把装有一键GHOST装系统的U盘插在电脑上,然后打开电脑马上按F2或DEL键入BIOS界面,然后就选择BOOT打USDHDD模式选择好,然后按F10键保存,电脑就会马上重启。2、重启后电脑就会进入一键…

    2022年9月6日
    2
  • Snapde一个全新的CSV超大文件编辑软件

    Snapde一个全新的CSV超大文件编辑软件今天介绍如果数据量超过104万行Excel无法打开了,用什么软件可以打开呢?Snapde,一个专门为编辑超大型数据量CSV文件而设计的单机版电子表格软件;它在C++语言开发的Snapman多人协作电子

    2022年7月1日
    24
  • Java创建文件和文件夹

    Java创建文件和文件夹例子。java代码:importjava.io.*;//导入所需的包publicclassIOTest{//类 publicstaticvoidmain(String[]args){//主程序,程序入口 Filefile=newFile(“D:\\Qiju_Li”); if(!file.exists()){//如果文件夹不存在 file.mk

    2022年6月18日
    28
  • 模板方法模式例子「建议收藏」

    模板方法模式例子「建议收藏」原文地址:http://www.cnblogs.com/jenkinschan/p/5768760.html一、概述 模板方法模式在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。二、结构类图三、解决问题模板方法就是提供一个算法框架,框架里面的步骤有些是父类已经定好的,有些需要子类自己实现。相当于要去办一件事情,行动的流

    2025年6月9日
    1
  • Java 正则表达式的用法和实例

    Java 正则表达式的用法和实例一、概述:用来描述或者匹配一系列符合某个语句规则的字符串二、单个符号1、英文句点”.”符号:匹配单个任意字符。表达式”t.o“可以匹配:tno,t#o,teo等等。不可以匹配:tnno,to,Tno,t正o等。2、中括号”[]“:只有方括号里面指定的字符才参与匹配,也只能匹配单个字符。表达式:t[abcd]n只可以匹配:tan,tbn,tcn,tdn。不可以匹配:th…

    2022年5月17日
    36

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号