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


相关推荐

  • crontab使用方法_crontab用法

    crontab使用方法_crontab用法crontab使用方法记录如下,解决了一些问题:crontab-e执行后弹出编辑窗口,输入如下内容:/2***sleep10;/root/do.sh>/dev/null2>&1表示每2分钟执行一次检测ssh可连通的脚本。注意,脚本输出到/dev/null,以避免crontab报警到root的邮件中。

    2022年8月24日
    3
  • Kubernetes 删除Evicted Pods命令[通俗易懂]

    Kubernetes 删除Evicted Pods命令[通俗易懂]安装jqsudoyuminstalljq-y运行删除命令kubectlgetpods–all-namespaces-ojson|jq’.items[]|select(.status.reason!=null)|select(.status.reason|contains(“Evicted”))|”kubectldeletepods\(.metadata.name)-n\(.metadata.namespace)”‘|xargs-n1bas

    2022年5月17日
    79
  • Laravel 队列发送邮件

    Laravel 队列发送邮件

    2021年10月25日
    34
  • icmp回复报文_常见的ICMP报文

    icmp回复报文_常见的ICMP报文常见的ICMP报文相应请求我们用的ping操作中就包括了相应请求(类型字段值为8)和应答(类型字段值为0)ICMP报文。过程:一台主机向一个节点发送一个类型字段值为8的ICMP报文,如果途中没有异常(如果没有被路由丢弃,目标不回应ICMP或者传输失败),则目标返回类型字段值为0的ICMP报文,说明这台主机存在。目标不可达,源抑制和超时报文这三种报文的格式是一样的。(1)目标不可到达报文(类型值为3…

    2022年5月1日
    123
  • onedrive免费扩容25t_onedrive怎么免费扩容1T

    onedrive免费扩容25t_onedrive怎么免费扩容1TOneDrive存储我们都知道没有开office365,自己onedrive的储存空间只有5GB,onenote做笔记以及用onedrive同步文档空间不够,但是又不想开office365;所以在网上看到别人说onedrive可以推荐别人注册,可以扩容10GB;加起来一共15GB,用来做笔记完全够用;或许有大佬会说可以弄到Office教育版的微软账号,有5T或1T的存储空间,但是这个会涉及到账号里面文档的安全性,这种账号是属于教育机构的,全局管理员可以有权查看里面储存的文件并且有权删去账号,这样的

    2022年9月10日
    0
  • 哈希表是哪一章节_哈希表的构造方法

    哈希表是哪一章节_哈希表的构造方法哈希表是个啥?小白:庆哥,什么是哈希表?这个哈希好熟悉,记得好像有HashMap和HashTable之类的吧,这是一样的嘛?????庆哥:这个哈希确实经常见????,足以说明它是个使用非常频繁的玩意儿,而且像你说的HashMap和HashTable之类的与哈希这个词肯定是有关系的,那哈希是个啥玩意啊,这个咱们还是得先来搞明白啥是个哈希表。????我们看看百科解释吧:散列表(Hashtable,也叫哈…

    2022年8月10日
    4

发表回复

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

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