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


相关推荐

  • wep激活成功教程——模拟chopchop攻击

    wep激活成功教程——模拟chopchop攻击在本文中 主要讲解了 chopchop 攻击的思想 用一道题目来感受 chopchop 攻击 该题目不是在于激活成功教程实际场景中的 wep 而是感受 chop 的思想 毕竟现在攻击 wep 的网上脚本比比皆是 比较著名的就是 Aircrack ng 无线激活成功教程工具套件 WEP WiredEquiVal 有线等效加密 其激活成功教程为利用加密体制缺陷 通过收集足够的数据包 使用分析密算法还原出密码 协议要求

    2026年3月16日
    2
  • spring整合mybatis详细步骤

    spring整合mybatis详细步骤spring整合mybatis的详细步骤

    2022年5月5日
    42
  • 如何对硬盘进行数据恢复_数据恢复用什么软件

    如何对硬盘进行数据恢复_数据恢复用什么软件怎样进行硬盘数据恢复硬盘上的数据如果不小心误删了怎么恢复?以下百分网小编整理的进行硬盘数据恢复的方法,希望对大家有所帮助,更多信息请关注应届毕业生网!1.下载运行DiskGenius数据恢复及磁盘分区软件。首先选择已删除文件所在的分区。然后点击工具栏按钮“恢复文件”,或点击主菜单“工具”中的“已删除或格式化后的文件恢复”菜单项,以打开文件恢复对话框。如下图:2.根据文件正常删除或格式化的不同情况,…

    2026年2月2日
    5
  • android画廊无限轮播,ViewPager无限循环实现画廊式banner

    android画廊无限轮播,ViewPager无限循环实现画廊式banner先看一下效果两边显示上一个和下一个item部分布局,可以自动滚动实现:布局主要属性:android:clipChildren=”false”//允许子布局超出父布局显示xml代码:Adapter没什么特别的只要getCount()返回一个很大的值如:returnInteger.MAX_VALUE;适配器代码:packagecom.guzhc.module_demo;importandr…

    2022年4月26日
    222
  • nc瑞士军刀详情

    nc瑞士军刀详情查看头文件nc-nv0.0.0.0IP地址80端口号(类telnet功能)head/聊天功能(传输是明文)nc相互传输文本信息(两台电脑实现类聊天功能)A:nc-lp4444

    2022年7月2日
    27
  • chegg网站_chunked

    chegg网站_chunked服务端给浏览器发送报文时,必须告诉浏览器报文的大小,这样浏览器可以根据报文大小来判断报文的完整性以及在长连接中确定报文的截尾。但是很多服务器的报文是动态创建的,在发送之前是无法确定其大小的。服务器只有等待内容全部创建后,计算出主体的大小,才能响应客户端的请求,这样的处理方法大大延迟了响应。传输编码中的分块编码为这种困难提供了解决方案,服务器可以逐块发送主体,并说明每块的大小就可以了。HTTP协议中

    2025年8月1日
    5

发表回复

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

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