My name Rithy.
I don't know how to add new line before appending a new node or element in the xml.
My php:
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = true;
$dom->load($xml_file);
$body = $dom->getElementsByTagName('body')->item(0);
$newelement_seg = $dom->createElement('seg');
$data = $dom->createTextNode(" text 2 ");
$newelement_seg->appendChild($data);
$body->appendChild($newelement_seg);
$dom->save($xml_file);
XML Before append a new child:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<seg>
text 1
</seg>
</body>
</xml>
XML after append a new child:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<seg>
text 1
</seg>
<seg>
text 2
</seg>
</body>
</xml>
But I want:
<?xml version="1.0" encoding="UTF-8"?>
<body>
<seg>
text 1
</seg>
<seg>
text 2
</seg>
</body>
</xml>
<hr/>
Thanks in advance!
TO SUMMARIZE THE ANSWERS GIVEN:
you have to set formatOutput to true
you have to set ignoreWhiteSpace to false
ie:
$dom = new DomDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->load($myxmlfile);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With