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


相关推荐

  • MYSQL分布式集群使用-主主复制「建议收藏」

    MYSQL分布式集群使用-主主复制

    2022年2月13日
    43
  • Nginx 单元测试自动化浅析之一-Test::Nginx源码分析和使用

    Nginx 单元测试自动化浅析之一-Test::Nginx源码分析和使用

    2022年2月23日
    50
  • c语言s16与u16_c语言中u8和u16是啥

    c语言s16与u16_c语言中u8和u16是啥在C语言中,并没有U16和S16这两种基本类型。不过在C语言的编程习惯上,往往为了简写,约定U16和S16两种类型。尤其常见于嵌入式编程或驱动编程上。其中U16为16位无符号数,S16为16为有符号数。定义如下:typedefshortS16;typedefunsignedshortU16;可以看到,U16和S16都是占2个字节的整型类型,区别只是是否有符号。于是U16可…

    2022年8月31日
    7
  • bootstraptable之uniqueId

    bootstraptable之uniqueId如何设置每行唯一的标识符uniqueId$(‘#dataTable’).bootstrapTable(‘destroy’).bootstrapTable({columns:[{field:’OrganizeID’,title:’部门编号’,…

    2025年7月29日
    5
  • 守护进程管理工具_进程保护工具

    守护进程管理工具_进程保护工具1.文本进程管理工具ntsysv是一个用户管理自动运行的守护进程的文本用户界面工具。2.命令行界面(CLI)工具可以使用chkconfig命令检查,设置系统的各种服务。此命令实际上是通过操作/etc/rc[0-6].d目录下的符号链接文件对系统的各种服务进行管理。chkconfig命令具有如下功能:1.添加指定的新服务2.清除指定的服务3.显示由chkconfig管理的服务4.改变服务的运行级别5…

    2025年6月4日
    3
  • 无证书签名简介[通俗易懂]

    无证书签名简介[通俗易懂]前言为什么要提出无证书签名防止公钥替换攻击防止密钥托管问题相关工作方案结构和安全模型方案结构参考:AnEfficientProvably-SecureCertificatelessSignatureSchemeforInternet-of-ThingsDeployment大致分为:Setup、Extract、KeyGen、Sign、Verify安全模型Al-Riyami2003Certificatelesspublickeycryptography提出的

    2022年4月30日
    111

发表回复

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

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