The new helper is similar to virXPathNodeSet list but for cases where we
want to get subelements directly rather than using XPath.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virxml.c | 34 ++++++++++++++++++++++++++++++++++
src/util/virxml.h | 5 +++++
3 files changed, 40 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 27b8e111aa..436d5a0770 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3709,6 +3709,7 @@ virXMLFormatMetadata;
virXMLNewNode;
virXMLNodeContentString;
virXMLNodeGetSubelement;
+virXMLNodeGetSubelementList;
virXMLNodeNameEqual;
virXMLNodeSanitizeNamespaces;
virXMLNodeToString;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index e04455fcaa..0dae15a039 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -899,6 +899,40 @@ virXMLNodeGetSubelement(xmlNodePtr node,
}
+/**
+ * virXMLNodeGetSubelementList:
+ * @node: node to get subelement of
+ * @name: name of subelement to fetch (NULL to fetch all sub-elements)
+ * @list: If non-NULL, filled with a list of pointers to the nodes. Caller is
+ * responsible for freeeing the list but not the members.
+ *
+ * Find and return a sub-elements node of @node named @name in a list.
+ * Returns the number of subelements with @name
+ */
+size_t
+virXMLNodeGetSubelementList(xmlNodePtr node,
+ const char *name,
+ xmlNodePtr **list)
+{
+ xmlNodePtr n;
+ size_t nelems = 0;
+
+ for (n = node->children; n; n = n->next) {
+ if (n->type == XML_ELEMENT_NODE) {
+ if (name && !virXMLNodeNameEqual(n, name))
+ continue;
+
+ if (list)
+ VIR_APPEND_ELEMENT_COPY(*list, nelems, n);
+ else
+ nelems++;
+ }
+ }
+
+ return nelems;
+}
+
+
/**
* virXPathNode:
* @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index cca9f222ab..7af47437bd 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -77,6 +77,11 @@ xmlNodePtr
virXMLNodeGetSubelement(xmlNodePtr node,
const char *name);
+size_t
+virXMLNodeGetSubelementList(xmlNodePtr node,
+ const char *name,
+ xmlNodePtr **list);
+
xmlNodePtr
virXPathNode(const char *xpath,
xmlXPathContextPtr ctxt);
--
2.40.1