xml缩进 [英] xml indentation

查看:211
本文介绍了xml缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用libxml2的xmlwriter API编写xml文件.

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

当我使用记事本打开文件时,缩进不正确.

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

有人知道如何解决吗?

非常感谢.

推荐答案

我在这里有点僵硬,但我要说的是,缩进不正确"表示您不正确完全缩进.

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默认情况下不会缩进您的XML文件,因为在XML中,空格(包括用于缩进的空格)是重要数据.也就是说,此XML文件:

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>

在语义上不同于:

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

...,因为第一个XML文件中的两个字符数据对您(程序员)来说很重要,因此XML在读取文件时将它们保留在那里,并且在编写时将不会输出(除非指示)

... 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.

也就是说,这是XML经常被滥用的地方.不幸的是,我见过的大多数XML都是缩进的. libxml2具有一些选项,可以在写入时自动输出缩进,而在读取时将其删除,但是请注意:有一些注意事项:它可能会弄错.文档说得更多.

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

请注意以下警告: libxml2常见问题解答

以及此处的信息: XML规范

这篇关于xml缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆