DOMDocument用换行符生成的XML [英] XML Generated by DOMDocument with Line Break

查看:386
本文介绍了DOMDocument用换行符生成的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP DOMDocument创建XML文件,并且这些XML文件不能包含换行符.

I am creating XML files with PHP DOMDocument, and these XML files can not contain line breaks.

但是当我使用方法" saveXML()"时,生成的XML在定义和初始标记之间带有换行符,如下所示:

But when I use the method "saveXML()", the generated XML comes with a line break between the definition and the initial tag, like this:

<?xml version="1.0" encoding="UTF-8"?>
<NFe xmlns="http://www.portalfiscal.inf.br/nfe"><infNFe...

我可以在DOMDocument中更正此问题吗?还是在生成XML之后必须这样做?

Can I correct this problem in DOMDocument? Or do I have to do it after I generate the XML?

我想更正此问题以获得以下结果:

I'd like to correct this problem to get a result like this:

<?xml version="1.0" encoding="UTF-8"?><NFe xmlns="...

推荐答案

默认情况下, DOMDocument::$formatOutput 是错误的.

By default, DOMDocument::$preserveWhiteSpace is true. Try setting it to false on the document in question, then calling saveXML again. This may have side effects should any whitespace inside the document actually matter. You should also make sure that DOMDocument::$formatOutput is false.

但是,正如戈登所说,对于空格的限制没有任何逻辑上的原因.尽管很认真,但是如果您不希望在其中留有任何空格,只需确保要保留的任何CR/LF字符都经过实体编码,然后$nonewlines = preg_replace("/[\r\n]/", '', $xml)提取掉在关闭Preserve和保留位置后可能保留的换行符.格式.但这又是.

As said by Gordon, though, there is no logical reason whatsoever for the whitespace restriction. Though seriously, if you don't want any whitespace in there whatsoever, just make sure any CR/LF characters that you want to keep are entity-encoded and then $nonewlines = preg_replace("/[\r\n]/", '', $xml) to yank out the newlines that might remain after turning off Preserve and Format. But again, that's silly.

这篇关于DOMDocument用换行符生成的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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