Add these helper functions to parse xml without using xmlXPathContext. Signed-off-by: Shi Lei <shi_lei@xxxxxxxxxxxxxx> --- 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 02b59ea..a2edbc1 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1405,3 +1405,60 @@ virXMLNamespaceRegister(xmlXPathContextPtr ctxt, return 0; } + + +/** + * 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; +} 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