Xml indent

I am writing an xml file using the xmlwriter api from libxml2.

when I open the file using notepad, the indentation is wrong.

Does anyone know how to fix it?

Thank you so much.

+6
xml libxml2
source share
2 answers

I'm going to cheat a little here, but I will say that with the "indent wrong" you mean that it has no indentation at all.

libxml2 by default will not backslide from your XML file, because in XML spaces (including those used for indentation) are significant data. That is, this XML file:

<root> <foo>Bar</foo> </root> 

semantically different from:

 <root><foo>Bar</foo></root> 

... is that two character parts of the data from the first XML file may be important to you, the programmer, so the XML leaves them there when it reads the file, and will not output them (if not specified) when writing.

However, this is too common an XML bit. Most of the XML I've seen is sadly indented. Libxml2 has options for automatically indenting when writing and deleting it when reading, but note: there are some caveats: this may be wrong. Documents say more.

I think this function can help you (I never used it myself :-)) if you want it to be indented for you: xmlTextWriterSetIndent

Listen to the warning here: libxml2 FAQ

And the information here: XML Specification

+9
source share

I believe that you only need indentation so that you can read the xml file ... in this case, you can now use google chrome to see the xml and it will already be shown with indentation (remember that this is not such a good idea indent xml file with libxml2).

-one
source share

All Articles