898951
From 2e65f52bf904f8ff5587dc42b7c37d94f9dab451 Mon Sep 17 00:00:00 2001
898951
Message-Id: <2e65f52bf904f8ff5587dc42b7c37d94f9dab451@dist-git>
898951
From: Peter Krempa <pkrempa@redhat.com>
898951
Date: Thu, 22 Jan 2015 15:53:42 +0100
898951
Subject: [PATCH] util: Add helper to convert libxml2 nodes to a string
898951
898951
https://bugzilla.redhat.com/show_bug.cgi?id=1184929
898951
898951
(cherry picked from commit be0f0c2292e3f171a031086f4d0a39b205c756a3)
898951
898951
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
898951
---
898951
 src/libvirt_private.syms |  1 +
898951
 src/util/virxml.c        | 33 +++++++++++++++++++++++++++++++++
898951
 src/util/virxml.h        |  2 ++
898951
 3 files changed, 36 insertions(+)
898951
898951
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
898951
index 61d8d26..3df4379 100644
898951
--- a/src/libvirt_private.syms
898951
+++ b/src/libvirt_private.syms
898951
@@ -2140,6 +2140,7 @@ virUUIDParse;
898951
 
898951
 # util/virxml.h
898951
 virXMLChildElementCount;
898951
+virXMLNodeToString;
898951
 virXMLParseHelper;
898951
 virXMLPickShellSafeComment;
898951
 virXMLPropString;
898951
diff --git a/src/util/virxml.c b/src/util/virxml.c
898951
index 4769569..9bb8bf0 100644
898951
--- a/src/util/virxml.c
898951
+++ b/src/util/virxml.c
898951
@@ -895,3 +895,36 @@ virXMLChildElementCount(xmlNodePtr node)
898951
     }
898951
     return ret;
898951
 }
898951
+
898951
+
898951
+/**
898951
+ * virXMLNodeToString: convert an XML node ptr to an XML string
898951
+ *
898951
+ * Returns the XML string of the document or NULL on error.
898951
+ * The caller has to free the string.
898951
+ */
898951
+char *
898951
+virXMLNodeToString(xmlDocPtr doc,
898951
+                   xmlNodePtr node)
898951
+{
898951
+     xmlBufferPtr xmlbuf = NULL;
898951
+     char *ret = NULL;
898951
+
898951
+     if (!(xmlbuf = xmlBufferCreate())) {
898951
+         virReportOOMError();
898951
+         return NULL;
898951
+     }
898951
+
898951
+     if (xmlNodeDump(xmlbuf, doc, node, 0, 1) == 0) {
898951
+         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
898951
+                        _("failed to convert the XML node tree"));
898951
+         goto cleanup;
898951
+     }
898951
+
898951
+     ignore_value(VIR_STRDUP(ret, (const char *)xmlBufferContent(xmlbuf)));
898951
+
898951
+cleanup:
898951
+     xmlBufferFree(xmlbuf);
898951
+
898951
+     return ret;
898951
+}
898951
diff --git a/src/util/virxml.h b/src/util/virxml.h
898951
index 364288d..bb34069 100644
898951
--- a/src/util/virxml.h
898951
+++ b/src/util/virxml.h
898951
@@ -163,4 +163,6 @@ int virXMLSaveFile(const char *path,
898951
                    const char *warnCommand,
898951
                    const char *xml);
898951
 
898951
+char *virXMLNodeToString(xmlDocPtr doc, xmlNodePtr node);
898951
+
898951
 #endif                          /* __VIR_XML_H__ */
898951
-- 
898951
2.2.1
898951