简体   繁体   English

libxml2属性修改C.

[英]libxml2 attribute modification C

I want to change one attribute ('voltage') in my xml: 我想在我的xml中更改一个属性('voltage'):

...
    <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 关于J.

Simply use xmlSetProp : 只需使用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; 如果@name有前缀,那么将使用相应的命名空间绑定(如果在范围内); it is an error it there's no such ns-binding for the prefix in scope. 这是一个错误,它在范围内没有这样的ns绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM