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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【15】进大厂必须掌握的面试题-容器化和虚拟化面试

    Q1。什么是容器? 我的建议是首先解释对容器化的需求,容器用于提供一致的计算环境,从开发人员的笔记本电脑到测试环境,从过渡环境到生产环境。 现在给出容器的定义,一个容器包含一个完整…

    2020年10月23日
    418
  • PyCharm安装步骤_jupyter运行py文件

    PyCharm安装步骤_jupyter运行py文件这里写自定义目录标题关于如何在pycharm安装jupyter关于如何在pycharm安装jupyterwindows+r打开命令窗口输入cmd使用如下命令(C:\Users\dd\PicturesDUPJ(18RS(4.输入jupyternotebook,把下面框出现的路径复制5.下图,然后ok…

    2022年8月25日
    8
  • HIBERNATE学习笔记

    HIBERNATE学习笔记

    2021年5月7日
    133
  • 《Qt 实战一二三》

    《Qt 实战一二三》Qt基础与实战,主要包含:环境搭建、信号槽、事件机制、数据类型、常用部件/布局、对话框、QPainter绘图等内容。

    2022年6月30日
    28
  • no jump_jump out

    no jump_jump outCprovidesaformofuser-levelexceptionalcontrolflow,calledanonlocaljump,thattransferscontroldirectlyfromonefunctiontoanothercurrentlyexecutingfunctionwithouthavingtogothroug

    2025年9月19日
    7
  • UFT工具简介

    UFT工具简介UFT UFT是一种自动测试工具。使用UFT的目的是想用它来执行重复的自动化测试,主要是用于回归测试和测试同一软件的新版本。因此你在测试前要考虑好如何对应用程序进行测试,例如要测试哪些功能、操作步骤、输入数据和期望的输出数据等  基本功能UFT提供符合所有主要应用软件环境的功能测试和回归测试的自动化。采用关键字驱动的理念以简化测试用例的创建和维护。它让用户可以直接录制屏幕上的操作流

    2022年5月15日
    169

发表回复

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

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