有没有办法增加PHP的DOMDocument formatOutput属性的缩进大小? [英] Is there a way to increase the indentation size of PHP's DOMDocument formatOutput property?

查看:118
本文介绍了有没有办法增加PHP的DOMDocument formatOutput属性的缩进大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以增加PHP的DOMDocument formatOutput属性的缩进大小?现在,它会将每个节点缩进2个空格。我想将其设置为制表符或4个空格。

Is there a way to increase the indentation size of PHP's DOMDocument formatOutput property? Right now it indents each node 2 spaces. I would like to make it either a tab or 4 spaces.

推荐答案

可以更改 libxml ,但是据我所知,您不能更改DOM使用的缩进。不过, XMLWriter 可能。

It might be possible to change the indentation string in libxml, but to my knowledge you cannot alter the indentation DOM uses. It's possible for XMLWriter though.

作为替代方案,您可以使用Tidy来漂亮地打印XML:

As an alternative, you could use Tidy to prettyprint the XML:

$dom = new DOMDocument;
$dom->preserveWhiteSpace = TRUE;
$dom->loadXml('<root><foo><bar>   baz   </bar></foo></root>');
$tidy = tidy_parse_string($dom->saveXml(), array(
    'indent' => TRUE,
    'input-xml' => TRUE,
    'output-xml' => TRUE,
    'add-xml-space' => FALSE,
    'indent-spaces' => 4
));
$tidy->cleanRepair();
echo $tidy;

但请注意,在上述情况下,这表现得很古怪。除非您在bar标签上拍 xml:space = preserve ,否则它将删除bar元素中的空格。当您这样做时,它将保留空格,但在前后都放置换行符。您必须弄弄它是否适合您的问题。请参见整理文档

but note that this behaves quirky in the above case. It removes the spaces in the bar element unless you slap an xml:space="preserve" on the bar tag. When you do that it will keep the spaces but also put newlines before and after. You have to fiddle with it to see if it fits your problem. See Tidy docs

这篇关于有没有办法增加PHP的DOMDocument formatOutput属性的缩进大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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