On a Tuesday in 2023, Michal Privoznik wrote:
When parsing an XML it may be important to keep indentation to
produce a better looking result when formatting the XML back.
Just look at all those xmlKeepBlanksDefault() calls just before
virXMLParse() is called.
Anyway, as of libxml2 commit v2.12.0~108 xmlKeepBlanksDefault()
is deprecated. Therefore, introduce virXMLParse...WithIndent()
variants which would do exactly xmlKeepBlanksDefault() did but
with non-deprecated APIs.
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/util/virxml.c | 15 +++++++++++++--
src/util/virxml.h | 29 +++++++++++++++++++++++++----
2 files changed, 38 insertions(+), 6 deletions(-)
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 31685cddde..e94708448a 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -1129,14 +1129,16 @@ virXMLParseHelper(int domcode,
const char *rootelement,
xmlXPathContextPtr *ctxt,
const char *schemafile,
- bool validate)
+ bool validate,
+ bool keepindent)
{
struct virParserData private;
g_autoptr(xmlParserCtxt) pctxt = NULL;
g_autoptr(xmlDoc) xml = NULL;
xmlNodePtr rootnode;
const char *docname;
- const int parseFlags = XML_PARSE_NONET |
+ int oldIndentTreeOutput = 0;
+ int parseFlags = XML_PARSE_NONET |
XML_PARSE_NOWARNING;
if (filename)
@@ -1155,12 +1157,21 @@ virXMLParseHelper(int domcode,
pctxt->_private = &private;
pctxt->sax->error = catchXMLError;
+ if (keepindent) {
+ parseFlags |= XML_PARSE_NOBLANKS;
+ xmlIndentTreeOutput = 1;
+ }
+
if (filename) {
xml = xmlCtxtReadFile(pctxt, filename, NULL, parseFlags);
} else {
xml = xmlCtxtReadDoc(pctxt, BAD_CAST xmlStr, url, NULL, parseFlags);
}
+ if (keepindent) {
+ xmlIndentTreeOutput = oldIndentTreeOutput;
+ }
+
Touching xmlIndentTreeOutput is not necessary.
1) it does not affect parsing, which is what we're doing here
2) xmlIndentTreeOutput = 1 is the default
All we need here is the XML_PARSE_NOBLANKS flag.
(Also, we did revert to the original value of xmlKeepBlanksDefaultValue
by calling xmlKeepBlanksDefault, but xmlIndentTreeOutput is always set
to 1 by xmlKeepBlanksDefault and never reset)
if (!xml) {
if (virGetLastErrorCode() == VIR_ERR_OK) {
virGenericReportError(domcode, VIR_ERR_XML_ERROR,
Reviewed-by: Ján Tomko <jtomko(a)redhat.com>
Jano