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)
上一篇 2022年7月17日 上午7:00
下一篇 2022年7月17日 上午7:00


相关推荐

  • 计算机设备问题代码43,设备管理器错误代码(代码43)的六种解决方法

    内容一、“由于此设备存在问题,Windows已将其停止(代码43)”),这是问题的原因原因分析:代码43错误是多个设备管理器错误代码之一。当设备管理器停止硬件设备时,会生成此错误,这可能是由硬件设备或设备驱动程序故障引起的。设备管理器错误代码(代码43)的详细信息可以在设备属性的“设备状态”区域中找到。引起问题的设备将在设备中用感叹号标记)设备管理器,如下图所示:有关如何解决此问题的信息,…

    2022年4月4日
    215
  • offsetwidth/clientwidth的区别「建议收藏」

    offsetwidth/clientwidth的区别「建议收藏」1.2.小小总结一小下”title=”clientWidth/scrollWidth/offsetWidth 小小总结一小下”style=”margin:0px;padding:0px;border:0px;list-style:none”>clientWidth是对象看到的宽度(不含边线,即border)scrollWidth是对象实际内容的宽度(若无p

    2022年7月22日
    22
  • 腾讯元宝ai入口网页版 腾讯元宝红包活动入口

    腾讯元宝ai入口网页版 腾讯元宝红包活动入口

    2026年3月13日
    1
  • 什么是linux Qt[通俗易懂]

    什么是linux Qt[通俗易懂]Qt是一个跨平台的C++图形用户界面库,由挪威TrollTech公司出品,目前包括Qt,基于Framebuffer的QtEmbedded,快速开发工具QtDesigner,国际化工具QtLinguist等部分Qt支持所有Unix系统,当然也包括Linux,还支持WinNT/Win2k,Win95/98平台。

    2022年5月17日
    35
  • python进阶(15)多线程与多进程效率测试[通俗易懂]

    python进阶(15)多线程与多进程效率测试[通俗易懂]前言在Python中,计算密集型任务适用于多进程,IO密集型任务适用于多线程正常来讲,多线程要比多进程效率更高,因为进程间的切换需要的资源和开销更大,而线程相对更小,但是我们使用的Python大多

    2022年7月28日
    9
  • tail -f 命令详解

    tail -f 命令详解tail命令可用于查看文件的内容,有一个常用的参数-f常用于查阅正在改变的日志文件。tail-f等同于–follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止tail-F等同于–follow=name–retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪t…

    2022年5月29日
    102

发表回复

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

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