[RFC 03/29] util: Add two helper functions virXMLChildNode and virXMLChildNodeSet

Shi Lei shi_lei at massclouds.com
Wed Mar 25 07:11:43 UTC 2020


When directive property 'args.noctxt" is set, there's no 'ctxt' argument
for the parse function. So RNG2C uses virXMLChildNode, virXMLChildNodeSet
and virXMLPropString to construct implementation of the parse function.

Signed-off-by: Shi Lei <shi_lei at massclouds.com>
---
 src/util/virxml.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virxml.h |  3 +++
 2 files changed, 60 insertions(+)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 9ea7b99..bfaed58 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -502,6 +502,63 @@ virXMLCheckIllegalChars(const char *nodeName,
 }
 
 
+/**
+ * virXMLChildNode:
+ * @node: Parent XML dom node pointer
+ * @name: Name of the child element
+ *
+ * Convenience function to return the child element of a XML node.
+ *
+ * Returns the pointer of child element node or NULL in case of failure.
+ * If there are many nodes match condition, it only returns the first node.
+ */
+xmlNodePtr
+virXMLChildNode(xmlNodePtr node, const char *name)
+{
+    xmlNodePtr cur = node->children;
+    while (cur) {
+        if (cur->type == XML_ELEMENT_NODE && virXMLNodeNameEqual(cur, name))
+            return cur;
+        cur = cur->next;
+    }
+    return NULL;
+}
+
+
+/**
+ * virXMLChildNodeSet:
+ * @node: Parent XML dom node pointer
+ * @name: Name of the children element
+ * @list: the returned list of nodes (or NULL if only count matters)
+ *
+ * Convenience function to evaluate a set of children elements
+ *
+ * Returns the number of nodes found in which case @list is set (and
+ *         must be freed) or -1 if the evaluation failed.
+ */
+int
+virXMLChildNodeSet(xmlNodePtr node, const char *name, xmlNodePtr **list)
+{
+    size_t count = 0;
+    if (list != NULL)
+        *list = NULL;
+
+    xmlNodePtr cur = node->children;
+    while (cur) {
+        if (cur->type == XML_ELEMENT_NODE && virXMLNodeNameEqual(cur, name)) {
+            if (list != NULL) {
+                if (VIR_APPEND_ELEMENT_COPY(*list, count, cur) < 0)
+                    return -1;
+            } else {
+                count++;
+            }
+        }
+        cur = cur->next;
+    }
+    return count;
+}
+
+
 /**
  * virXMLPropString:
  * @node: XML dom node pointer
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 26ab9f9..0abde44 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -79,6 +79,9 @@ char *     virXMLPropStringLimit(xmlNodePtr node,
 char *   virXMLNodeContentString(xmlNodePtr node);
 long     virXMLChildElementCount(xmlNodePtr node);
 
+xmlNodePtr virXMLChildNode(xmlNodePtr node, const char *name);
+int virXMLChildNodeSet(xmlNodePtr node, const char *name, xmlNodePtr **list);
+
 /* Internal function; prefer the macros below.  */
 xmlDocPtr      virXMLParseHelper(int domcode,
                                  const char *filename,
-- 
2.17.1






More information about the libvir-list mailing list