函数xmlFree() 仅释放由某些库函数分配的内存,这不是您要搜索的。
尝试使用例如xmlSetNsProp():
xmlAttrPtr xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar * name, const xmlChar * value)
设置(或重置)节点携带的属性。 ns 结构必须在作用域内,这个不检查
节点:节点
ns:命名空间定义
name:属性名称
value:属性值
返回:属性指针。
您将在此处找到更多信息:http://xmlsoft.org/html/libxml-tree.html
而且我认为您可以找到最适合您需求的功能。
在源代码中,命名空间似乎是ns->href:
/**
* xmlSetNsProp:
* @node: the node
* @ns: the namespace definition
* @name: the attribute name
* @value: the attribute value
*
* Set (or reset) an attribute carried by a node.
* The ns structure must be in scope, this is not checked
*
* Returns the attribute pointer.
*/
xmlAttrPtr xmlSetNsProp(xmlNodePtr node, xmlNsPtr ns, const xmlChar *name, const xmlChar *value)
{
xmlAttrPtr prop;
if(ns && (ns->href == NULL))
return (NULL);
prop = xmlGetPropNodeInternal(node, name, (ns != NULL) ? ns->href : NULL, 0);
if(prop != NULL)
{
/*
* Modify the attribute's value.
*/
if(prop->atype == XML_ATTRIBUTE_ID)
{
xmlRemoveID(node->doc, prop);
prop->atype = XML_ATTRIBUTE_ID;
}
if(prop->children != NULL)
xmlFreeNodeList(prop->children);
prop->children = NULL;
prop->last = NULL;
prop->ns = ns;
if(value != NULL)
{
xmlNodePtr tmp;
if(!xmlCheckUTF8(value))
{
xmlTreeErr(XML_TREE_NOT_UTF8, (xmlNodePtr)node->doc,
NULL);
if (node->doc != NULL)
node->doc->encoding = xmlStrdup(BAD_CAST "ISO-8859-1");
}
prop->children = xmlNewDocText(node->doc, value);
prop->last = NULL;
tmp = prop->children;
while(tmp != NULL)
{
tmp->parent = (xmlNodePtr)prop;
if(tmp->next == NULL)
prop->last = tmp;
tmp = tmp->next;
}
}
if(prop->atype == XML_ATTRIBUTE_ID)
xmlAddID(NULL, node->doc, value, prop);
return (prop);
}
/*
* No equal attr found; create a new one.
*/
return (xmlNewPropInternal(node, ns, name, value, 0));
}