util: Add helper to convert libxml2 nodes to a string

This commit is contained in:
Peter Krempa 2013-09-04 16:49:40 +02:00
parent e9cb66f14e
commit be0f0c2292
3 changed files with 36 additions and 0 deletions

View File

@ -2092,6 +2092,7 @@ virUUIDParse;
# util/virxml.h
virXMLChildElementCount;
virXMLNodeToString;
virXMLParseHelper;
virXMLPickShellSafeComment;
virXMLPropString;

View File

@ -895,3 +895,36 @@ virXMLChildElementCount(xmlNodePtr node)
}
return ret;
}
/**
* virXMLNodeToString: convert an XML node ptr to an XML string
*
* Returns the XML string of the document or NULL on error.
* The caller has to free the string.
*/
char *
virXMLNodeToString(xmlDocPtr doc,
xmlNodePtr node)
{
xmlBufferPtr xmlbuf = NULL;
char *ret = NULL;
if (!(xmlbuf = xmlBufferCreate())) {
virReportOOMError();
return NULL;
}
if (xmlNodeDump(xmlbuf, doc, node, 0, 1) == 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("failed to convert the XML node tree"));
goto cleanup;
}
ignore_value(VIR_STRDUP(ret, (const char *)xmlBufferContent(xmlbuf)));
cleanup:
xmlBufferFree(xmlbuf);
return ret;
}

View File

@ -163,4 +163,6 @@ int virXMLSaveFile(const char *path,
const char *warnCommand,
const char *xml);
char *virXMLNodeToString(xmlDocPtr doc, xmlNodePtr node);
#endif /* __VIR_XML_H__ */