需要在每个xml元素后添加回车符(php)

I have a file that is sent over https and I am handling with a php script.

The data is accepted using:

$data = file_get_contents('php://input')

If written to a file it is written as one line.

Due to our internal systems (IBM power 7) I'm told by I.T that I need to add a carriage return after each xml element.

So the file currently opens in an editor as :

<root><element1><element2></element2></element1></root>

I need it to be :

<root>
<element1>
<element2></element2>
</element1>
</root>

Which requires inserting " " after each closing tag and a tag with children.

Any ideas?

The formatOutput option will do it.

If it's just for inserting line-breaks, a regex will do fine.

However, do NOT start parsing XML with Regular Expressions!

Try this:

$xml = preg_replace(
         '=(<(.*?)>)(?![^<>]*</\2>|$)=s', 
         "\\1
", 
         file_get_contents('php://input') ); 

The expression matches all XML tags that are not followed by EOF or a matching closing tag using a negative lookahead assertion [(?!..)] and a backreference.