Introduce a simple helper fetching a sub-element node by name. This is
meant as a simple replacement for either open-coded versions of this or
use of XPath for this trivial lookup.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virxml.c | 23 +++++++++++++++++++++++
src/util/virxml.h | 5 +++++
3 files changed, 29 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ebd7bc61a8..209ec14544 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -3671,6 +3671,7 @@ virXMLFormatElementInternal;
virXMLFormatMetadata;
virXMLNewNode;
virXMLNodeContentString;
+virXMLNodeGetSubelement;
virXMLNodeNameEqual;
virXMLNodeSanitizeNamespaces;
virXMLNodeToString;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 870ba194b0..7d9d4bb234 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -849,6 +849,29 @@ virXPathBoolean(const char *xpath,
}
+/**
+ * virXMLNodeGetSubelement:
+ * @node: node to get subelement of
+ * @name: name of subelement to fetch
+ *
+ * Find and return a sub-element node of @node named @name.
+ */
+xmlNodePtr
+virXMLNodeGetSubelement(xmlNodePtr node,
+ const char *name)
+{
+ xmlNodePtr n;
+
+ for (n = node->children; n; n = n->next) {
+ if (n->type == XML_ELEMENT_NODE &&
+ virXMLNodeNameEqual(n, name))
+ return n;
+ }
+
+ return NULL;
+}
+
+
/**
* virXPathNode:
* @xpath: the XPath string to evaluate
diff --git a/src/util/virxml.h b/src/util/virxml.h
index f19cbe59ae..d5b998263c 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -72,6 +72,11 @@ int
virXPathLongLong(const char *xpath,
xmlXPathContextPtr ctxt,
long long *value);
+
+xmlNodePtr
+virXMLNodeGetSubelement(xmlNodePtr node,
+ const char *name);
+
xmlNodePtr
virXPathNode(const char *xpath,
xmlXPathContextPtr ctxt);
--
2.38.1