libxml2 attribute modification C

你说的曾经没有我的故事 提交于 2019-12-23 18:16:53

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/19053316/libxml2-attribute-modification-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!