PHP to generate XML empty element shorthand problem

When you generate an XML time, if that is to say when it will return an empty element of it is empty of the tag <test />, how to convert <test> </test>it?

In XML, they are considered to be the exact same thing, any parser should recognize both forms. However, sometimes this is the case required format through the LIBXML_NOEMPTYTAGconstant passed to DOMdocumentthe saveXML()change its methods.

How to display two markers in the space-time node content ???

<?php
$dom     = new \DOMDocument('1.0');
$document = $dom->createElement('document');
$document = $dom->appendChild($document);
$head = $dom->createElement('title','this is title');
$content = $dom->createElement('content','');
$document->appendChild($head);
$document->appendChild($content);
echo $dom->saveXML();
?>

In XML, they are considered to be the exact same thing, any parser should recognize both forms. However, if you still need, you can write

<?php
echo $dom->saveXML($dom->documentElement, LIBXML_NOEMPTYTAG);
?>
输出1:
<?xml version="1.0"?>
<document>
    <title>this is title</title>
    <content/>
</document>

输出2:
<document>
    <title>this is test</title>
    <content></content>
</document>

Example of use:
DOMDocument :: saveXML

Published 41 original articles · won praise 21 · views 70000 +

Guess you like

Origin blog.csdn.net/u010324331/article/details/86512009