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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

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