Search code examples
cxmllibxml2

Remove white space between xmldocument and root node in libxml2 c


I have created a xml tree using libxml2 C. For example, my output is

<?xml version="1.0" encoding="UTF-8"?>`  

<Ocean>                                 

    <Sea>Pacific</Sea>

    <Lake>Tahoe</Lake>

</Ocean>

I have used xmlDocDumpMemoryEnc(....) to remove node indenting and then my output is

<?xml version="1.0" encoding="UTF-8"?>

<Ocean><Sea>Pacific</Sea><Lake>Tahoe</Lake></Ocean>

However I want my output as

<?xml version="1.0" encoding="UTF-8"?><Ocean><Sea>Pacific</Sea><Lake>Tahoe</Lake></Ocean>

I have searched libxml2 module functions to remove white space between xmldocument and root node and found none. I greatly appreciate any help on this.


Solution

  • While not mandated by the standard, it's common to have a new line after the XML declaration.

    libxml2 has this new line hardcoded, so you are left with either, having libxml2 skip the XML declaration and prepending it yourself or output it and shifting it right to overwrite the line break.