The new helper is similar to virXPathNode list but for cases where we
want to get subelements directly rather than using XPath.

Signed-off-by: Peter Krempa <pkre...@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virxml.c        | 29 +++++++++++++++++++++++++++++
 src/util/virxml.h        |  4 ++++
 3 files changed, 34 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f1bed27ba7..951b3691cf 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3691,6 +3691,7 @@ virXMLFormatMetadata;
 virXMLNewNode;
 virXMLNodeContentString;
 virXMLNodeGetSubelement;
+virXMLNodeGetSubelementList;
 virXMLNodeNameEqual;
 virXMLNodeSanitizeNamespaces;
 virXMLNodeToString;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 9b6ccfd6c9..7c853a591a 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -872,6 +872,35 @@ virXMLNodeGetSubelement(xmlNodePtr node,
 }


+/**
+ * virXMLNodeGetSubelementList:
+ * @node: node to get subelement of
+ * @name: name of subelement to fetch (NULL to fetch all sub-elements)
+ *
+ * Find and return a sub-elements node of @node named @name in a GPtrArray
+ * populated with the xmlNodePtr objects. Caller is responsible for freeing the
+ * array but not the contained xmlNode objects.
+ */
+GPtrArray *
+virXMLNodeGetSubelementList(xmlNodePtr node,
+                            const char *name)
+{
+    GPtrArray *ret = g_ptr_array_new();
+    xmlNodePtr n;
+
+    for (n = node->children; n; n = n->next) {
+        if (n->type == XML_ELEMENT_NODE) {
+            if (name && !virXMLNodeNameEqual(n, name))
+                continue;
+
+            g_ptr_array_add(ret, n);
+        }
+    }
+
+    return ret;
+}
+
+
 /**
  * virXPathNode:
  * @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index d5b998263c..46a4550788 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -77,6 +77,10 @@ xmlNodePtr
 virXMLNodeGetSubelement(xmlNodePtr node,
                         const char *name);

+GPtrArray *
+virXMLNodeGetSubelementList(xmlNodePtr node,
+                            const char *name);
+
 xmlNodePtr
 virXPathNode(const char *xpath,
              xmlXPathContextPtr ctxt);
-- 
2.39.2

Reply via email to