mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-24 14:45:24 +00:00
util: xml: Introduce virXMLFormatElementEmpty
Add a helper which will format an XML element with attributes and children, but compared to virXMLFormatElement it also formats an empty element if both buffers are empty. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Andrea Bolognani <abologna@redhat.com> Tested-by: Andrea Bolognani <abologna@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
6c7faa2720
commit
1ac21ab7ea
@ -3541,6 +3541,7 @@ virXMLBufferCreate;
|
||||
virXMLCheckIllegalChars;
|
||||
virXMLExtractNamespaceXML;
|
||||
virXMLFormatElement;
|
||||
virXMLFormatElementEmpty;
|
||||
virXMLNewNode;
|
||||
virXMLNodeContentString;
|
||||
virXMLNodeNameEqual;
|
||||
|
@ -1631,6 +1631,32 @@ virXMLValidatorFree(virXMLValidator *validator)
|
||||
}
|
||||
|
||||
|
||||
/* same as virXMLFormatElement but outputs an empty element if @attrBuf and
|
||||
* @childBuf are both empty */
|
||||
void
|
||||
virXMLFormatElementEmpty(virBuffer *buf,
|
||||
const char *name,
|
||||
virBuffer *attrBuf,
|
||||
virBuffer *childBuf)
|
||||
{
|
||||
virBufferAsprintf(buf, "<%s", name);
|
||||
|
||||
if (attrBuf && virBufferUse(attrBuf) > 0)
|
||||
virBufferAddBuffer(buf, attrBuf);
|
||||
|
||||
if (childBuf && virBufferUse(childBuf) > 0) {
|
||||
virBufferAddLit(buf, ">\n");
|
||||
virBufferAddBuffer(buf, childBuf);
|
||||
virBufferAsprintf(buf, "</%s>\n", name);
|
||||
} else {
|
||||
virBufferAddLit(buf, "/>\n");
|
||||
}
|
||||
|
||||
virBufferFreeAndReset(attrBuf);
|
||||
virBufferFreeAndReset(childBuf);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* virXMLFormatElement
|
||||
* @buf: the parent buffer where the element will be placed
|
||||
@ -1655,21 +1681,7 @@ virXMLFormatElement(virBuffer *buf,
|
||||
(!childBuf || virBufferUse(childBuf) == 0))
|
||||
return;
|
||||
|
||||
virBufferAsprintf(buf, "<%s", name);
|
||||
|
||||
if (attrBuf && virBufferUse(attrBuf) > 0)
|
||||
virBufferAddBuffer(buf, attrBuf);
|
||||
|
||||
if (childBuf && virBufferUse(childBuf) > 0) {
|
||||
virBufferAddLit(buf, ">\n");
|
||||
virBufferAddBuffer(buf, childBuf);
|
||||
virBufferAsprintf(buf, "</%s>\n", name);
|
||||
} else {
|
||||
virBufferAddLit(buf, "/>\n");
|
||||
}
|
||||
|
||||
virBufferFreeAndReset(attrBuf);
|
||||
virBufferFreeAndReset(childBuf);
|
||||
virXMLFormatElementEmpty(buf, name, attrBuf, childBuf);
|
||||
}
|
||||
|
||||
|
||||
|
@ -315,6 +315,13 @@ virXMLFormatElement(virBuffer *buf,
|
||||
virBuffer *attrBuf,
|
||||
virBuffer *childBuf);
|
||||
|
||||
void
|
||||
virXMLFormatElementEmpty(virBuffer *buf,
|
||||
const char *name,
|
||||
virBuffer *attrBuf,
|
||||
virBuffer *childBuf);
|
||||
|
||||
|
||||
struct _virXPathContextNodeSave {
|
||||
xmlXPathContextPtr ctxt;
|
||||
xmlNodePtr node;
|
||||
|
Loading…
Reference in New Issue
Block a user