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


相关推荐

  • 多国语言在线客服系统源码+软件下载二合一集成

    多国语言在线客服系统源码+软件下载二合一集成  本文分三部分系统介绍如何开发一套在线客服系统聊天源码,该源码基于ThinkPHP,代码完全开源。  首先,我们只使用@auth指令。  其次,我们添加一个带有参数的订阅类型。  第三,我们更新@auth指令和订阅类型。  完整源码:kf.zxkfym.top  1使用@auth指令并执行身份验证  添加和使用身份验证$amplifyaddauthScanningforplugins…PluginscansuccessfulUsingservice:Cog

    2022年7月19日
    29
  • 使用C#开发屏幕保护程序步骤建议收藏

    本文介绍使用C#制作屏幕保护的方法,这个屏幕保护就是仿效视窗系统自带的字幕屏保。屏幕保护程序的扩展名虽然是"scr",但其实是一个可执行的"exe"文件。但他

    2021年12月20日
    45
  • 简述MD5加密[通俗易懂]

    简述MD5加密[通俗易懂]MD5加密publicclassMD5{ /**四个链接变量标准幻数(按大端字节序存储-高位字节排放在内存的低地址端(即该值的起始地址),低位字节排放在内存的高地址端)*/privatefinalintA=0x67452301;//01234567privatefinalintB=0xefcdab89;//89abcdef…

    2022年7月11日
    21
  • 基于flask框架的高校舆情分析系统[通俗易懂]

    基于flask框架的高校舆情分析系统[通俗易懂]系统分析:高校舆情分析拟实现如下功能,采集微博、贴吧、学校官网的舆情信息,对这些舆情进行数据分析、情感分析,提取关键词,生成词云分析,情感分析图,实时监测舆情动态。系统设计:前端:采用layui+echarts实现图表的展示,数据分析的结果后端:采用requests实现数据的采集,利用flask+mysql搭建web网站框架,利用机器学习的中文分词、情感分析等技术生成词云分析、关键词提取、情感分析等功能系统难点:采集微博、贴吧的数据,利用机器学习的知识生成词云分析、情感分析系统实现如

    2022年9月20日
    2
  • Map转对象_json对象转map

    Map转对象_json对象转mapjava对象转map:https://blog.csdn.net/to_Date32/article/details/78190785

    2022年5月3日
    30
  • soap 设置header 添加头元素 webservice wsdl

    soap 设置header 添加头元素 webservice wsdl

    2021年5月4日
    152

发表回复

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

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