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


相关推荐

  • 报表数据填报中的合法性校验

    报表数据填报中的合法性校验

    2021年6月18日
    115
  • SQLserver字符串转日期_sqlserver 字符串转时间

    SQLserver字符串转日期_sqlserver 字符串转时间CONVERT(varchar(100),CAST(‘20190102’ASdatetime),23)以上将字符串转为2019-01-02这样的格式。

    2022年10月8日
    1
  • win10JDK环境变量的配置

    win10JDK环境变量的配置在win10系统下进行jdk环境变量与win7的一些区别,作者踩过一些坑,最后通过网上查找资料才将问题解决,但是网上的资料比较零碎,作者整理一下亲自操作过程并且将步骤详细的写了下来,希望能够帮助需要的人。一、下载安装完成jdk之后,桌面右键此电脑选择属性,进入以下界面,然后选择高级系统设置,进入系统配置二、进入系统配置之后点击环境变量,进入环境变量配置窗口三、此时我们可以看到有用户变量…

    2022年7月21日
    9
  • TextView 使用自定义的字体和亮点

    TextView 使用自定义的字体和亮点

    2021年12月17日
    51
  • pycharm2022 02激活码【2022最新】

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

    2022年4月1日
    111
  • linux搭建apache_plsql安装教程及配置

    linux搭建apache_plsql安装教程及配置在CentOS上安装ApacheApache在默认的CentOS存储库中可用,并且安装非常简单。为了安装ApacheWebServer,首先通过运行以下命令更新本地软件包在基于RHEL的发行版中,Apache软件包和服务称为httpd。要安装Apache,运行以下命令:[root@localhost]#sudoyumupdate更新完成后,就可以安装Apache了。[root@localhost]#yuminstallhttpd启动ApacheWeb服..

    2022年9月21日
    2

发表回复

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

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