XML Generated by DOMDocument with Line Break

有些话、适合烂在心里 提交于 2019-12-12 13:17:11

问题


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

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

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

回答1:


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.

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.



来源:https://stackoverflow.com/questions/5277120/xml-generated-by-domdocument-with-line-break

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