From 2e65f52bf904f8ff5587dc42b7c37d94f9dab451 Mon Sep 17 00:00:00 2001
Message-Id: <2e65f52bf904f8ff5587dc42b7c37d94f9dab451@dist-git>
From: Peter Krempa <pkrempa@redhat.com>
Date: Thu, 22 Jan 2015 15:53:42 +0100
Subject: [PATCH] util: Add helper to convert libxml2 nodes to a string
https://bugzilla.redhat.com/show_bug.cgi?id=1184929
(cherry picked from commit be0f0c2292e3f171a031086f4d0a39b205c756a3)
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virxml.c | 33 +++++++++++++++++++++++++++++++++
src/util/virxml.h | 2 ++
3 files changed, 36 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 61d8d26..3df4379 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2140,6 +2140,7 @@ virUUIDParse;
# util/virxml.h
virXMLChildElementCount;
+virXMLNodeToString;
virXMLParseHelper;
virXMLPickShellSafeComment;
virXMLPropString;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 4769569..9bb8bf0 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -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;
+}
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 364288d..bb34069 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -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__ */
--
2.2.1