libxml2的属性修改Ç [英] libxml2 attribute modification C

查看:142
本文介绍了libxml2的属性修改Ç的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的XML来改变一个属性('压')

  ...
    <检测电压=1.2e3f/>
...

下面是我尝试如何做到这一点:

 无效save_detector_attr(*的xmlNode节点,xmlDoc中* DOC){
        字符*电压;
        xmlAttrPtr s_vnewattr;
        CHAR BUF [128];
        *的xmlNode CUR =&于节点GT; xmlChildrenNode;
        浮SV;
        INT CNT = 0;
        而(CUR!= NULL){
            如果(当前>类型== XML_ELEMENT_NODE){
                如果(xmlStrcmp(电流>!名字探测器)){
                    电压= xmlGetProp(CUR,电压);
                    SV = ATOF(电压);                    的snprintf(BUF,128,%F,SV + 20.1);                    s_vnewattr = xmlNewProp(CUR,电压,BUF);
                    的printf(保存到的电压值:%S \\ n,BUF);                }
            }
            CUR =当前>接下来,
        }
        xmlSaveFor​​matFile(./mc2x.xml,DOC,1);

不幸的是,而不是重写属性,调用函数时我在与这个新的文件后:

 <检测电压=1.2e3f电压=1220.100000/>

如何使该属性将rewriten而不是创建一个新的?

问候
Ĵ


解决方案

只需使用 xmlSetProp

  xmlAttrPtr xmlSetProp(的xmlNodePtr节点,
                       常量XMLCHAR *名称,
                       常量XMLCHAR *值)


  

设置(或重置)由一个节点携带的属性。如果@name具有preFIX,则相应的空间结合将被使用,如果在范围;这是一个错误它有没有这样的NS-结合在范围preFIX。


I want to change one attribute ('voltage') in my xml:

...
    <detector voltage="1.2e3f"/>
...

Here is how im trying to do it:

 void save_detector_attr(xmlNode *node, xmlDoc *doc){
        char *voltage;
        xmlAttrPtr s_vnewattr;
        char buf[128];
        xmlNode *cur = node->xmlChildrenNode;
        float sv;
        int cnt = 0;
        while(cur != NULL) {
            if (cur->type == XML_ELEMENT_NODE) {
                if (!xmlStrcmp(cur->name, "detector")){
                    voltage = xmlGetProp(cur, "voltage");
                    sv = atof(voltage);

                    snprintf(buf, 128, "%f", sv + 20.1 );

                    s_vnewattr = xmlNewProp (cur, "voltage", buf);
                    printf(" SAVING to voltage value: %s\n", buf);

                }
            }
            cur = cur->next;
        }
        xmlSaveFormatFile ("./mc2x.xml", doc, 1);

unfortunately, instead of rewriting attribute, after that function is called i'm having a new file with this:

<detector voltage="1.2e3f" voltage="1220.100000"/>

How to make that attribute will be rewriten instead of creating a new one ?

regards J

解决方案

Simply use xmlSetProp:

xmlAttrPtr xmlSetProp (xmlNodePtr node, 
                       const xmlChar * name, 
                       const xmlChar * value)

Set (or reset) an attribute carried by a node. If @name has a prefix, then the corresponding namespace-binding will be used, if in scope; it is an error it there's no such ns-binding for the prefix in scope.

这篇关于libxml2的属性修改Ç的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆