util: xml: Introduce virXMLNodeGetSubelementList

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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-02-13 15:53:23 +01:00
parent 4aafa900ce
commit dcd49d2cd6
3 changed files with 40 additions and 0 deletions

View File

@ -3709,6 +3709,7 @@ virXMLFormatMetadata;
virXMLNewNode;
virXMLNodeContentString;
virXMLNodeGetSubelement;
virXMLNodeGetSubelementList;
virXMLNodeNameEqual;
virXMLNodeSanitizeNamespaces;
virXMLNodeToString;

View File

@ -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 freeing 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

View File

@ -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);