Rithy Rithy - 1 year ago 111
PHP Question

PHP DOM append child with new line

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!

Answer Source

What are you trying to accomplish?

By setting preserveWhiteSpace to true (unnecessary; that's the default), you are telling libxml not to ignore text nodes that are composed only of white space. Yet, at the same time, you're trying to pretty format the XML file, which to be really pretty requires whitespace nodes.

Then, even ignoring white space, you have to realize the whitespace inside the <seg> tags is significant; libxml will not remove those; if you force a line break after text 1, in the next line the </seg> closing tag must not be indented, otherwise the content of the text node inside the tag would differ.

Your code gives me this:

<?xml version="1.0" encoding="UTF-8"?>
<body>
<seg>
  text 1  
</seg>
<seg> text 2 </seg></body>

Since you're telling libxml that whitespace is significant, it cannot put a line break after the second </seg>, otherwise it'd be creating another text node.

If you say $dom->preserveWhiteSpace = false;:

<?xml version="1.0" encoding="UTF-8"?>
<body>
  <seg>
  text 1  
</seg>
  <seg> text 2 </seg>
</body>

The tags are indented, but libxml still cannot do this:

<?xml version="1.0" encoding="UTF-8"?>
<body>
  <seg>
    text 1  
  </seg>
  <seg>
    text 2
  </seg>
</body>

because it would be changing the content of the text nodes inside <seg>.

You might want to try tidy, though I'm not sure it'll do what you want.