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


相关推荐

  • scrapy爬虫完整的代码实例[通俗易懂]

    scrapy爬虫完整的代码实例[通俗易懂]新建工程scrapystartprojecttutorial进入tutorial目录,在spider下面新建quotes_spider.pyimportscrapyfrom..itemsimportQuotesItem#coding:utf-8classQuotesSpider(scrapy.Spider):name=”quot…

    2022年6月26日
    32
  • idea2021.1.3激活码【2021免费激活】

    (idea2021.1.3激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS32PGH0SQB-eyJsaWN…

    2022年3月26日
    2.7K
  • 从K近邻算法、距离度量谈到KD树、SIFT+BBF算法

    从K近邻算法、距离度量谈到KD树、SIFT+BBF算法从K近邻算法、距离度量谈到KD树、SIFT+BBF算法前言前两日,在微博上说:“到今天为止,我至少亏欠了3篇文章待写:1、KD树;2、神经网络;3、编程艺术第28章。你看到,blog内的文章与你于别处所见的任何都不同。于是,等啊等,等一台电脑,只好等待..”。得益于田,借了我一台电脑(借他电脑的时候,我连表示感谢,他说“能找到工作全靠你的博客,这点儿小忙还说,不地道”,有的时候,稍许感受到

    2022年6月6日
    23
  • Http响应头里Cache-Control:no-cache、max-age=””和no-store

    Http响应头里Cache-Control:no-cache、max-age=””和no-store响应头:Cache-Control:no-cache,强制每次请求直接发送给源服务器,而不经过本地缓存版本的校验。这对于需要确认认证应用很有用(可以和public结合使用),或者严格要求使用最新数据 的应用(不惜牺牲使用缓存的所有好处)通俗解释:浏览器通知服务器,本地没有缓存数据//======================================================…

    2022年6月13日
    32
  • Python网络爬虫实战项目代码大全「建议收藏」

    Python网络爬虫实战项目代码大全「建议收藏」WechatSogou [1]-微信公众号爬虫。基于搜狗微信搜索的微信公众号爬虫接口,可以扩展成基于搜狗搜索的爬虫,返回结果是列表,每一项均是公众号具体信息字典。 DouBanSpider [2]-豆瓣读书爬虫。可以爬下豆瓣读书标签下的所有图书,按评分排名依次存储,存储到Excel中,可方便大家筛选搜罗,比如筛选评价人数&gt;1000的高分书籍;可依据不同的主题存储到Excel不同的Shee…

    2022年5月20日
    34
  • win10键锁定计算机,win10系统创建一键锁定计算机的快捷方式的操作方法

    win10键锁定计算机,win10系统创建一键锁定计算机的快捷方式的操作方法win10系统创建一键锁定计算机的快捷方式的操作方法?很多win10用户在使用电脑的时候,会发现win10系统创建一键锁定计算机的快捷方式的的现象,根据小编的调查并不是所有的朋友都知道win10系统创建一键锁定计算机的快捷方式的的问题怎么解决,不会的朋友也不用担心,下面我就给大家讲解一下win10系统创建一键锁定计算机的快捷方式的的少许解决办法,其实步骤很简单,只需要1、首先在桌面上的空白处鼠标右…

    2022年7月21日
    20

发表回复

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

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