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

相关推荐

  • dock是什么意思中文_电脑dock是什么

    dock是什么意思中文_电脑dock是什么Dock从初级到高级全套教程dock是iMac中很常见的一个功能,而且非常实用,那么dock到底是什么意思呢?1.经常使用苹果电脑的小伙伴,和大家讨论时总会听到有人在说dock栏,不过还是有很多小伙伴不知道dock到底是什么意思,接下来小编就给大家普及一下。2.dock一般指的就是苹果操作系统中的停靠栏,也就是苹果电脑桌面中最下方,类似于Windows系统下方的任务栏,不过要比Window…

    2022年9月12日
  • 八路抢答器一个数码管C语言,八路抢答器设计 – 八路抢答器电路设计方案汇总(五款模拟电路设计原理及工作原理详细)…「建议收藏」

    八路抢答器一个数码管C语言,八路抢答器设计 – 八路抢答器电路设计方案汇总(五款模拟电路设计原理及工作原理详细)…「建议收藏」八路抢答器电路设计方案四:一个简单的的八路抢答器电路详细电路设计方案:八路抢答器电路图八路抢答器电路设计方案五:基于74LS148和74LS297的八路抢答器设计要求与内容在许多比赛活动中,为了准确、公正、直观地判断出第一抢答者,通常设置一台抢答器,通过数显、灯光或音响等多种手段指示出第一抢答者。(1)设计制作一个可容纳8组参赛的数字式抢答器,每组设置一个抢答按钮供抢答者使用。(2)电路具有第一抢…

    2022年10月20日
  • Oracle:varchar和varchar2的区别

    Oracle:varchar和varchar2的区别Oracle:varchar和varchar2的区别 1.varchar2把所有字符都占两字节处理(一般情况下),varchar只对汉字和全角等字符占两字节,数字,英文字符等都是一个字节;2.varchar2把空串等同于null处理,而varchar仍按照空串处理;3.varchar2字符要用几个字节存储,要看数据库使用的字符集. 然后char和varchar2的区别是

    2022年6月18日
  • idea连接mysql数据库报java.lang.RuntimeException: com.mysql.cj.exceptions.InvalidConnectionAttributeExcepti

    idea连接mysql数据库报java.lang.RuntimeException: com.mysql.cj.exceptions.InvalidConnectionAttributeExcepti出错场景:使用idea连接mysql(5.2版本)报错:java.lang.RuntimeException:com.mysql.cj.exceptions.InvalidConnectionAttributeException:Theservertimezonevalue’Öйú±ê׼ʱ¼ä’isunrecognizedorrepresentsmoreth…

    2022年7月24日
  • redflag linux7,RedFlag Linux7 update firefox 3.52

    redflag linux7,RedFlag Linux7 update firefox 3.52RedFlagLinux7updatefirefox3.52红旗LINUX7更新FIREFOX3.5.2操作步骤:1、用psaux|grepfirxfox查看firefox详细路径[@more@][sam@samhost~]$psaux|grepfirefoxsam27600.31.61166449612?Sl2…

    2022年8月20日
  • navicat12激活码在线生成(JetBrains全家桶)

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

    2022年3月27日

发表回复

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

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