rapidxml操作XML

rapidxml操作XML主要对上一篇文章做了修改,文章涉及创建、读取和修改XML文件,内容比较齐全,可以供大家学习。创建xml文件:基本步骤:给文件分配节点xmlDoc.allocate_node(node_element,”seqs”,NULL);把分配好的节点添加到文件中xmlDoc.append_node(seqsNode)。对于节点属性,先分配节点xml_node<>*seqsNode=xmlDoc

大家好,又见面了,我是你们的朋友全栈君。

主要对上一篇文章做了修改,文章涉及创建、读取和修改XML文件,内容比较齐全,可以供大家学习。

创建xml文件:
基本步骤:给文件分配节点xmlDoc.allocate_node(node_element,”seqs”,NULL);把分配好的节点添加到文件中xmlDoc.append_node(seqsNode)。
对于节点属性,先分配节点xml_node<>* seqsNode = xmlDoc.allocate_node(node_element,”seqs”,NULL);,然后给节点添加属性seqNode->append_attribute(xmlDoc.allocate_attribute(“name”,”a”)),最后在把带属性的节点添加到文件中seqsNode->append_node(seqNode)。

#include <iostream> 
#include "rapidxml/rapidxml.hpp" 
#include "rapidxml/rapidxml_utils.hpp" 
#include "rapidxml/rapidxml_print.hpp" 

using namespace rapidxml; 

int main()
{
    xml_document<> xmlDoc; 
    xml_node<>* rot = xmlDoc.allocate_node(rapidxml::node_pi,xmlDoc.allocate_string("xml version='1.0' encoding='utf-8'")); 
    xmlDoc.append_node(rot); 

    xml_node<>* seqsNode = xmlDoc.allocate_node(node_element,"seqs",NULL);
    xmlDoc.append_node(seqsNode); 

    xml_node<>* seqNode = xmlDoc.allocate_node(node_element,"seq",NULL);
    seqNode->append_attribute(xmlDoc.allocate_attribute("name","a")); 
    seqNode->append_attribute(xmlDoc.allocate_attribute("license","1")); 
    seqNode->append_attribute(xmlDoc.allocate_attribute("enable","true")); 
    seqsNode->append_node(seqNode); 

    xml_node<>* seqNode1 = xmlDoc.allocate_node(node_element,"seq",NULL);
    seqNode1->append_attribute(xmlDoc.allocate_attribute("name","b")); 
    seqNode1->append_attribute(xmlDoc.allocate_attribute("license","1")); 
    seqNode1->append_attribute(xmlDoc.allocate_attribute("enable","true")); 
    seqsNode->append_node(seqNode1); 

    std::string text; 
    rapidxml::print(std::back_inserter(text), xmlDoc, 0); 

    std::cout<<text<<std::endl; 

    std::ofstream out("E:/UIMRIS/BRANCHES/uMR_MAIN/MAIN/Features/win32test/XMLFile6.xml"); 
    if (!out.good())
    {
        std::cout<<"error";
    }
    out << text; 
    out.close();

    std::cout << "seccessful";

    getchar();
    return 0;
}

读取xml文件:
基本步骤:首先打开文件;然后获取节点xml_node<>* seqsNode = xmlDoc.first_node(“seqs”);最后获得属性xml_attribute<>* attrSeq = seqNode->first_attribute(“name”)。

#include <iostream> 
#include "rapidxml/rapidxml.hpp" 
#include "rapidxml/rapidxml_utils.hpp" 
#include "rapidxml/rapidxml_print.hpp" 
#include "boost/property_tree/xml_parser.hpp"

using namespace rapidxml; 

struct SeqStruct
{
    std::string seqName;
    std::string seqLicense;
    std::string seqEnable;
};

int main()
{
    SeqStruct seqs;
    std:: vector<SeqStruct> vecSeq;
    std::vector <std::string> vecStringSeq;

    file<> fDoc("E:/UIMRIS/BRANCHES/uMR_MAIN/MAIN/Features/win32test/XMLFile6.xml");
    std::cout << fDoc.data() << std::endl;
    xml_document<> xmlDoc;
    xmlDoc.parse<0>(fDoc.data());

    xml_node<>* seqsNode = xmlDoc.first_node("seqs");
    for (xml_node<>* seqNode = seqsNode->first_node(); seqNode != NULL; seqNode = seqNode->next_sibling())
    {
        xml_attribute<>* attrSeq = seqNode->first_attribute("name");
        std::cout<<"name: "<< attrSeq->value() <<" ";
        std::string sTempName = attrSeq->value();

        attrSeq = attrSeq->next_attribute("license");
        std::cout<<"license: "<< attrSeq->value()<<" ";

        attrSeq = attrSeq->next_attribute("enable");
        std::cout<<"enable: "<< attrSeq->value()<< std::endl;

        std::string sTempEnable = attrSeq->value();

        if ("true" == sTempEnable)
        {
            vecStringSeq.push_back(sTempName);
        }   
    }

    for (int i = 0; i < vecStringSeq.size(); ++i)
    {
        std::cout << vecStringSeq[i] << std::endl;
    }

    getchar();

    return 0;
}

设置xml部分属性:
基本要点:读取要修改的属性然后修改attrSeq->value(xmlDoc.allocate_string(vecSeq[i].seqLicense.c_str()))。

#include <iostream> 
#include "rapidxml/rapidxml.hpp" 
#include "rapidxml/rapidxml_utils.hpp" 
#include "rapidxml/rapidxml_print.hpp" 
#include "boost/property_tree/xml_parser.hpp"

using namespace rapidxml; 

struct SeqStruct
{
    std::string seqName;
    std::string seqLicense;
    std::string seqEnable;
};

int main()
{
    SeqStruct seqs;
    std:: vector<SeqStruct> vecSeq;

    seqs.seqName = "a";
    seqs.seqLicense = "1";
    seqs.seqEnable = "true";
    vecSeq.push_back(seqs);
    std::cout << "enable: "<< vecSeq[0].seqEnable << std::endl;
    seqs.seqName = "c";
    seqs.seqLicense = "0";
    seqs.seqEnable = "false";
    vecSeq.push_back(seqs);

    for(int i = 0; i < 2; i++)
    {
        std::cout <<vecSeq[i].seqName << " ";
        std::cout <<vecSeq[i].seqLicense << " ";
        std::cout <<vecSeq[i].seqEnable << " ";

        std::cout << std::endl;
    }

    file<> fDoc("E:/UIMRIS/BRANCHES/uMR_MAIN/MAIN/Features/win32test/XMLFile6.xml");
    std::cout << fDoc.data() << std::endl;
    xml_document<> xmlDoc;
    xmlDoc.parse<0>(fDoc.data());

    xml_node<>* seqsNode = xmlDoc.first_node("seqs");

    for (xml_node<>* seqNode = seqsNode->first_node(); seqNode != NULL; seqNode = seqNode->next_sibling())
    {
        bool bFlag = true;

        xml_attribute<>* attrSeq = seqNode->first_attribute("name");
        std::string sTem = attrSeq->value();
        for (int i = 0; i < vecSeq.size(); ++i)
        {           
            if (vecSeq[i].seqName.c_str() == sTem)
            {
                attrSeq = attrSeq->next_attribute();
                attrSeq->value(xmlDoc.allocate_string(vecSeq[i].seqLicense.c_str()));

                bFlag = false;
                break;
            }
        }
        if (bFlag)
        {
            attrSeq = attrSeq->next_attribute();
            attrSeq->value(xmlDoc.allocate_string("0"));
        }
    }

    std::string text;
    rapidxml::print(std::back_inserter(text), xmlDoc, 0);    
    std::cout<<text<<std::endl;   
    std::ofstream out("E:/UIMRIS/BRANCHES/uMR_MAIN/MAIN/Features/win32test/XMLFile7.xml");  
    if (!out.good())
    {
        std::cout<<"error";
    }
    out << text;  
    out.close();

    getchar();

    return 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160921.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Robo 3T安装及使用

    Robo 3T安装及使用Robo3T 是一款叫 mongodb 可视化工具 是一个免费版本 还有个付费版本叫 Studio3T 下载地址 https robomongo org download 选择适合你的 我选择 exe 下载好后解压 创建快捷键 打开窗口再 ctrl N 快捷键打开连接窗口 点击 connect 连接连接成功 会显示 admin local config test 四个数据库 你之前创建的一般保存在 test 设置时间显示在未设置前数据内的时间数都比当前时间少 8 小时 比如 2019 2 2715

    2025年10月17日
    2
  • 镁光(Micron)存储器根据丝印找具体型号(datasheet)的方法[通俗易懂]

    镁光(Micron)存储器根据丝印找具体型号(datasheet)的方法[通俗易懂]镁光(Micron)存储器根据丝印找具体型号(datasheet)的方法

    2022年6月22日
    100
  • jquery 判断array中是否包含指定字符串

    jquery 判断array中是否包含指定字符串

    2021年7月19日
    66
  • LinkedList浅析

    LinkedList浅析LinkedList简介LinkedList是一个继承于AbstractSequentialList的双向链表。它也可以被当作堆栈、队列或双端队列进行操作。LinkedList实现List接口,能对它进行队列操作。LinkedList实现Deque接口,即能将LinkedList当作双端队列使用。LinkedList实现了Cloneable接口,即覆盖了函数clon…

    2022年6月29日
    32
  • 步入J2EE架构和过程「建议收藏」

    步入J2EE架构和过程「建议收藏」标 题:步入J2EE架构和过程发信站:BBS水木清华站(FriApr2616:02:082002)本文转自赛迪网开发者developer.ccidnet.com摘要Java2企业版(J2EE)平台由四个关键部分构成:规格说明、参考实现、兼容性测试套件和蓝图(BluePrint)计划。蓝图描绘了分布式组件架构最好的实践和设计指导方针。本文基于Rational统一过程和Bl

    2025年6月20日
    5
  • pycharm 2021.9激活码_最新在线免费激活

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

    2022年3月26日
    45

发表回复

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

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