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


相关推荐

  • spdlog linux编译出错,【C++】spdlog–log4cxx有点笨重,试一试spdlog

    spdlog linux编译出错,【C++】spdlog–log4cxx有点笨重,试一试spdlogspdlog是什么linuxFastC++logginglibrary按照官方介绍,是一个高性能的C++日志组件,支持跨平台,兼容C++11。原来项目中使用的是log4cxx,我感受稍微有点笨重,而且好久没有更新了。ios在新项目中,我只须要一款轻量级的日志组件,能:git存文件按照天数切分快速的浏览了spdlog,知足个人需求,因而开搞!githubspdlog快速入门如下内容来自spd…

    2022年6月23日
    52
  • 延时函数如何延时

    延时函数如何延时延时函数如何延时**函数样例编译软件编译后的汇编指令Delay_ms(200);…

    2022年6月22日
    32
  • Android sdk_安卓sdk工具下载

    Android sdk_安卓sdk工具下载1在官网上获得软件压缩包imx-android-10.0.0_2.5.0.tar.gz下载地址2获取源码拷贝imx-android-10.0.0_2.5.0.tar.gz到一个文件夹里并解压,进入:2.1获取repogitclonehttps://mirrors.tuna.tsinghua.edu.cn/git/git-repo修改repo里的内容为下面REPO_URL=’https://mirrors.bfsu.edu.cn/git/git-repo’然后添

    2022年8月30日
    3
  • Java链表——遍历、查找、求链表长度

    Java链表——遍历、查找、求链表长度1.遍历非常简单的一段代码,只需要在节点不为空时,一个接一个地输出即可。publicvoidErgodic(){ ListNodeindexNode=head; while(indexNode.getNext()!=null){ System.out.print(indexNode.getVal()+””); indexNode=indexNode.getNext(); } }2.查找我们来做一个对值的查找…

    2022年5月13日
    64
  • 寄存器,移位寄存器的电路原理以及verilog代码实现「建议收藏」

    寄存器,移位寄存器的电路原理以及verilog代码实现「建议收藏」寄存器:用以存放二进制代码的电路,下图为由维特阻塞D触发器组成的4位数码寄存器:逻辑功能分析:1.异步端CR置0时,输出置0;2.同步并行置数:D0~D3为4个输入代码,当CP上升沿到达时,D0

    2022年7月1日
    99
  • MQTTnet[通俗易懂]

    MQTTnet[通俗易懂]近期学习了一下物联网中应用较广的MQTT协议,同时使用MQTTnet开源类库做了简单实现,因此做下笔记。注意:在实现订阅者离线再连接时,一直接受不到离线信息,需要做一下配置源码>>>GitHub//Broker设置options.MaxPendingMessagesPerClient=1000;options.EnablePersistentSessions…

    2022年6月25日
    41

发表回复

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

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