开发者

php->xml using DOM, prints all on the same line, server config or intended?

开发者 https://www.devze.com 2022-12-19 11:40 出处:网络
I\'m using php DOM to build an XML file of data, it works fine but it all outputs on one line, like so:

I'm using php DOM to build an XML file of data, it works fine but it all outputs on one line, like so:

<property><something><somethingelse>dfs</somethingelse></something></property>

However in all examples I've found it's outputting properly, like so:

<property>
    <something>
       <somethingelse>
           dfs
       </somethingelse>
    </something>
</property>

Now I know I can force a new line with \n and \t for tab but I don't want to do this unless necessary. So, my question:

Is my server config (for some reason) forcing it to output on the same line,开发者_如何学运维 or is something else happening? All examples of this in use show no new line usage and they show new lines, so I think something is wrong.

I am declaring

header('Content-Type: text/xml');


Set the formatOutput attribute in your DOMDocument object to true:

$domDocument->formatOutput = true;


By default, DOM won't add extra whitespace to your document. You can ask it to by setting

$doc->formatOutput = true;

See the corresponding entry in the manual.

It will not add whitespace to text nodes though, so the "dfs" in your example will not get indented this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号