util: xml: Introduce virXMLNodeGetSubelement

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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2022-12-02 10:35:13 +01:00
parent a5911fd808
commit 1a136152e6
3 changed files with 29 additions and 0 deletions

View File

@ -3671,6 +3671,7 @@ virXMLFormatElementInternal;
virXMLFormatMetadata;
virXMLNewNode;
virXMLNodeContentString;
virXMLNodeGetSubelement;
virXMLNodeNameEqual;
virXMLNodeSanitizeNamespaces;
virXMLNodeToString;

View File

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

View File

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