xml indentation

只谈情不闲聊 提交于 2019-12-19 18:26:16

问题


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

when I open the file using notepad, the indentation isn't right.

does anybody know how to fix it?

thanks a whole lot.


回答1:


I'm going on a bit of a limb here, but I'll say that by "the indentation isn't right" you mean it isn't indented at all.

libxml2, by default, won't indent your XML file, because in XML, whitespace (including that used for indentation) is significant data. That is, this XML file:

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

is semantically different from:

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

... in that the two character data pieces from the first XML file could be significant to you, the programmer, so, XML leaves them in there when reading the file, and will not output them (unless instructed) when writing.

That said, this is an oft-abused bit of XML. Most XML I've seen is, sadly, indented. libxml2 has options for automatically outputting the indentation when writing, and removing it when reading, but note: there are some caveats to this: it might get it wrong. The docs say more.

I think this function might help you (I've never actually used this myself :-) ), if you want it to indent for you: xmlTextWriterSetIndent

Heed the warning here: libxml2 FAQ

And the information here: XML Specification




回答2:


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



来源:https://stackoverflow.com/questions/4537038/xml-indentation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!