Libxml2 save empty tags

libxml2 (for C) does not save empty elements in their original form when saved. It replaces <tag></tag> with <tag/> , which is technically correct, but causes problems for us.

 xmlDocPtr doc = xmlParseFile("myfile.xml"); xmlNodePtr root = xmlSaveFile("mynewfile.xml", doc); 

I tried playing with various options (using xlmReadFile ), but no one affects the output. One of the posts mentioned disabling tags, but the example was for PERL, and I did not find an analogue for C.

Is it possible to disable this behavior?

+3
source share
2 answers

Just found this enum in xmlsave documentation:

  Enum xmlSaveOption { XML_SAVE_FORMAT = 1 : format save output XML_SAVE_NO_DECL = 2 : drop the xml declaration XML_SAVE_NO_EMPTY = 4 : no empty tags XML_SAVE_NO_XHTML = 8 : disable XHTML1 specific rules XML_SAVE_XHTML = 16 : force XHTML1 specific rules XML_SAVE_AS_XML = 32 : force XML serialization on HTML doc XML_SAVE_AS_HTML = 64 : force HTML serialization on XML doc XML_SAVE_WSNONSIG = 128 : format with non-significant whitespace } 

Perhaps you can reorganize the application to use this module for serialization and play a little with these parameters. Especially with XML_SAVE_NO_EMPTY .

+3
source

Your code might look like this:

 xmlSaveCtxt *ctxt = xmlSaveToFilename("mynewfile.xml", "UTF-8", XML_SAVE_FORMAT | XML_SAVE_NO_EMPTY); if (!ctxt || xmlSaveDoc(ctxt, doc) < 0 || xmlSaveClose(ctxt) < 0) //...deal with the error 
0
source

All Articles