RapidXML的读写

RapidXML的读写把如下图几个文件放到工程目录(hpp文件)新建工程进行读写测试,代码如下://ConsoleApplication1.cpp:定义控制台应用程序的入口点。//#include”stdafx.h”#include”rapidxml.hpp”#include”rapidxml_utils.hpp”//rapidxml::file#include”rapidx…

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

把如下图几个文件放到工程目录(hpp文件)

RapidXML的读写

新建工程进行读写测试,代码如下:

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include “stdafx.h”

#include “rapidxml.hpp”
#include “rapidxml_utils.hpp”  //rapidxml::file
#include “rapidxml_print.hpp”  //rapidxml::print
#include <windows.h>
#include <iostream>

 

void writeFile(const char * file_name)
{

    char buf[1024] = { 0 };
    rapidxml::xml_document<> doc;
    // XML头的声明
    rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration);
    declaration->append_attribute(doc.allocate_attribute(“version”, “1.0”));
    declaration->append_attribute(doc.allocate_attribute(“encoding”, “utf-8”));
    doc.append_node(declaration);
    rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, “root”);
    doc.append_node(root);
    rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, “all students info”);
    root->append_node(comment1);
    rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, “students”);
    for (int i = 0; i < 10; ++i)
    {

        rapidxml::xml_node<>* n1 = doc.allocate_node(rapidxml::node_element, “student”);
        // doc.allocate_string 的作用是将源字符串深拷贝一份
        n1->append_attribute(doc.allocate_attribute(“name”, doc.allocate_string(buf)));
        n1->append_attribute(doc.allocate_attribute(“score”, doc.allocate_string(std::to_string(100 – i).c_str())));
        students->append_node(n1);
    }
    root->append_node(students);
    std::ofstream outfile(file_name, std::ios::out);
    if (outfile)
    {

        std::string text;
        rapidxml::print(std::back_inserter(text), doc, 0);
        outfile << text;
        outfile.close();
    }
}

void readFile(const char * fileName)
{

    std::ifstream inf(fileName, std::ios::in);
    if (!inf)
    {

        return;
    }

    inf.seekg(0, std::ios::end);
    int nLen = inf.tellg();
    inf.seekg(0, std::ios::beg);
    char * strc = new char[nLen+1];
    ZeroMemory(strc, nLen + 1);
    inf.read(strc, nLen);
    rapidxml::xml_document<> doc;
    doc.parse<0>(strc);
    rapidxml::xml_node<> *root = doc.first_node(“root”);
    int nSize = 0;
    
    rapidxml::xml_node<>* child = root->first_node(“students”)->first_node();
    while (child)
    {

            //判断属性是否存在
        rapidxml::xml_attribute<>* nameAttr = child->first_attribute(“name”);
        rapidxml::xml_attribute<>* scoreAttr = child->first_attribute(“score”);
        char *nameC;
        char *scoreC;
        if (nameAttr)
        {

            nameC = nameAttr->value();
        }
        if (scoreAttr)
        {

            scoreC = scoreAttr->value();
        }
        child = child->next_sibling();
    }

    /*for (rapidxml::xml_node<>* child = root->first_node(“students”)->first_node(); child; child = child->next_sibling())
    {

        char *nameC = child->first_attribute(“name”)->value();
        char * homea = child->first_attribute(“score”)->value();
    }*/

    delete strc;
    strc = NULL;
    
}

int _tmain(int argc, _TCHAR* argv[])
{

    writeFile(“11.xml”);
    readFile(“11.xml”);
    return 0;
}

 

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

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

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


相关推荐

  • phpstorm 2022.01.13 激活【2021最新】

    (phpstorm 2022.01.13 激活)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~0HKLM1UCCY-eyJsaWNlbnNlSWQiOi…

    2022年3月31日
    196
  • lambda表达式用法_使用lambda表达式定义函数

    lambda表达式用法_使用lambda表达式定义函数(一)输入参数在Lambda表达式中,输入参数是Lambda运算符的左边部分。它包含参数的数量可以为0、1或者多个。只有当输入参数为1时,Lambda表达式左边的一对小括弧才可以省略。输入参数的数量大于或者等于2时,Lambda表达式左边的一对小括弧中的多个参数质检使用逗号(,)分割。示例1下面创建一个Lambda表达式,它的输入参数的数量为0.该表达式将显示“ThisisaLambdae…

    2022年9月19日
    2
  • python之lambda函数/表达式[通俗易懂]

    python之lambda函数/表达式[通俗易懂]lambda函数也叫匿名函数,允许快速定义单行函数。通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数。格式lambda与def的区别1)def创建的方法是有名称的

    2022年7月5日
    30
  • Windows技术篇——进程、线程、消息机制进程间通信[通俗易懂]

    Windows技术篇——进程、线程、消息机制进程间通信[通俗易懂]概念192.168.0.1–192.168.0.255一、进程状态1、创建状态:进程由创建而产生。2、就绪状态:指进程已准备好运行状态,即进程已分配到除CPU以外所有的必要资源后,只要再获得CPU,合可立即执行。(有执行资格,没有执行权的进程)3、运行状态:指进程已经获取CPU,其进程处于正在执行的状态。(既有执行资格,又有执行权的进程)4、阻塞状态:指正在执行的进程由于发生某事件(如…

    2022年8月18日
    14
  • Spring+MyBatis实例详解「建议收藏」

    Spring+MyBatis实例详解「建议收藏」1.项目结构:                2.项目的Maven依赖:&lt;properties&gt; &lt;spring-version&gt;4.3.21.RELEASE&lt;/spring-version&gt; &lt;/properties&gt; &lt;dependencies&gt; &lt;dependen…

    2022年6月24日
    21
  • python字符串转义字符_python 转义

    python字符串转义字符_python 转义上图中因为python不知到如何处理一对单引号之后的内容,不能识别第三个单引号。(默认都是以一对单引号或双引号来表示字符串从结束到开始。)长字符串:用三个引号(单引号或者双引号)来代表字符串开始和结束例如在有些情况下,就需要用转义:(1)>>>path=’c:\nowhere’输出的内容换行,不是想要的结果,我们就需要转义用反斜杠\来转义…

    2025年8月13日
    4

发表回复

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

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