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@xxxxxxxxxx> --- src/libvirt_private.syms | 1 + src/util/virxml.c | 42 ++++++++++++++++++++++++++-------------- src/util/virxml.h | 7 +++++++ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b3f9c9681a..64002de39a 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3541,6 +3541,7 @@ virXMLBufferCreate; virXMLCheckIllegalChars; virXMLExtractNamespaceXML; virXMLFormatElement; +virXMLFormatElementEmpty; virXMLNewNode; virXMLNodeContentString; virXMLNodeNameEqual; diff --git a/src/util/virxml.c b/src/util/virxml.c index 117f50f2bf..c2a49cbef2 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1314,6 +1314,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 @@ -1338,21 +1364,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); } diff --git a/src/util/virxml.h b/src/util/virxml.h index de171dce12..a81db478f0 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -226,6 +226,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; -- 2.30.2