mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 17:34:18 +03:00
tools: add helper method for printing an XML document
The trivial case of fully printing an XML document is boring, but this helper does more by allowing an XPath expression to be given. It will then print just the subset of nodes which match the expression. It either print each match as a standalone XML doc or can put them into one new XML doc wrapped woith <nodes>...</nodes> Reviewed-by: Michal Privoznik <mprivozn@redhat.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
812edc95a3
commit
8615c19b5d
@ -436,3 +436,62 @@ virshDomainBlockJobToString(int type)
|
||||
const char *str = virshDomainBlockJobTypeToString(type);
|
||||
return str ? _(str) : _("Unknown job");
|
||||
}
|
||||
|
||||
bool
|
||||
virshDumpXML(vshControl *ctl,
|
||||
const char *xml,
|
||||
const char *url,
|
||||
const char *xpath,
|
||||
bool wrap)
|
||||
{
|
||||
g_autoptr(xmlDoc) doc = NULL;
|
||||
g_autoptr(xmlXPathContext) ctxt = NULL;
|
||||
g_autofree xmlNodePtr *nodes = NULL;
|
||||
int nnodes = 0;
|
||||
size_t i;
|
||||
int oldblanks;
|
||||
|
||||
if (xpath == NULL) {
|
||||
vshPrint(ctl, "%s", xml);
|
||||
return true;
|
||||
}
|
||||
|
||||
oldblanks = xmlKeepBlanksDefault(0);
|
||||
doc = virXMLParseString(xml, url);
|
||||
xmlKeepBlanksDefault(oldblanks);
|
||||
if (!doc)
|
||||
return false;
|
||||
|
||||
if (!(ctxt = virXMLXPathContextNew(doc)))
|
||||
return false;
|
||||
|
||||
if ((nnodes = virXPathNodeSet(xpath, ctxt, &nodes)) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wrap) {
|
||||
g_autoptr(xmlDoc) newdoc = xmlNewDoc((xmlChar *)"1.0");
|
||||
xmlNodePtr newroot = xmlNewNode(NULL, (xmlChar *)"nodes");
|
||||
g_autofree char *xmlbit = NULL;
|
||||
|
||||
xmlDocSetRootElement(newdoc, newroot);
|
||||
|
||||
for (i = 0; i < nnodes; i++) {
|
||||
g_autoptr(xmlNode) copy = xmlDocCopyNode(nodes[i], newdoc, 1);
|
||||
if (!xmlAddChild(newroot, copy))
|
||||
return false;
|
||||
|
||||
copy = NULL;
|
||||
}
|
||||
|
||||
xmlbit = virXMLNodeToString(doc, newroot);
|
||||
vshPrint(ctl, "%s\n", xmlbit);
|
||||
} else {
|
||||
for (i = 0; i < nnodes; i++) {
|
||||
g_autofree char *xmlbit = virXMLNodeToString(doc, nodes[i]);
|
||||
vshPrint(ctl, "%s\n", xmlbit);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -156,3 +156,10 @@ VIR_ENUM_DECL(virshDomainBlockJob);
|
||||
|
||||
const char *
|
||||
virshDomainBlockJobToString(int type);
|
||||
|
||||
bool
|
||||
virshDumpXML(vshControl *ctl,
|
||||
const char *xml,
|
||||
const char *url,
|
||||
const char *xpath,
|
||||
bool wrap);
|
||||
|
Loading…
Reference in New Issue
Block a user