对齐xml文档

inp*_*put 2 php xml domdocument data-structures

我正在使用php domdocument将数据插入到xml文件中.但是当我打开xml文件时,数据显示在一行中:

<?xml version="1.0"?>
<root><activity>swimming</activity><activity>jogging</activity></root>
Run Code Online (Sandbox Code Playgroud)

我如何以编程方式对齐它?

<?xml version="1.0"?>
<root>
  <activity>swimming</activity>
  <activity>jogging</activity>
</root>
Run Code Online (Sandbox Code Playgroud)

jcu*_*bic 6

您可以使用此功能

function pretty_xml($string) {
  $xml = DOMDocument::loadXML($string);
  $xml->formatOutput = true; 
  return $xml->saveXML();
}