0

I am using a DOMDocument to add new nodes to an XML file using PHP. I am using the createElement() and appendChild() functions.

The problem is that the indentation of the tags in the XML is lost. How can I keep the indentation in the XML or re-indent after addition of a node?

My XML before modification is nicely indented:

<my_xml>
   <level>Some level</level>
</my_xml>

But after modification, the indentation is lost:

<my_xml>
   <level>Some level</level>
<level>New Level</level></my_xml>
dr_rk
  • 4,395
  • 13
  • 48
  • 74

1 Answers1

0

You can do this before you get the XML:

$dom->formatOutput = true;
Rômulo M. Farias
  • 1,483
  • 1
  • 15
  • 29
  • Is the `$doc->preserveWhiteSpace = false;` also needed, in the documentation it says that setting it to `false` means 'Do not remove redundant white space'? Surely, indentation must rely on redundant whitespaces? – dr_rk Mar 08 '16 at 15:48