我想在我的 xml 中更改一个属性(“电压”):
...
<detector voltage="1.2e3f"/>
...
这是我尝试这样做的方法:
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);
不幸的是,在调用该函数之后,我没有重写属性,而是有一个新文件:
<detector voltage="1.2e3f" voltage="1220.100000"/>
如何使该属性被重写而不是创建一个新属性?
问候J