How to define the output format of an XML in PHP?

1

Good afternoon!

I'm developing a webservice in which I will not disclose the name in which there is a method that accepts XML in the following way:

<exemplo>
   <exemplo></exemplo>
   <exemplo></exemplo>
</exemplo>

I am creating the XML's by DOMDocument but I can not generate the XML's as above, it always comes out in that format the children that do not have values: <exemplo/> and this generates errors in webservice not returning what I need.

Is there any way for this output to be generated by DOM?

Follow how I raise children.

$exemplo = $dom->createElement("exemplo","");
$root->appendChild($exemplo);
    
asked by anonymous 05.09.2016 / 20:47

1 answer

1

Do as follows, using the LIBXML_NOEMPTYTAG option as the second parameter in the saveXML method:

<?php

$dom = new DOMDocument( "1.0", "ISO-8859-15" );

$root = $dom->createElement("root","");

$exemplos = $dom->createElement("exemplo","");

$exemplo1 = $dom->createElement("exemplo","");
$exemplo2 = $dom->createElement("exemplo","");
$exemplo3 = $dom->createElement("exemplo","");

$exemplos->appendChild($exemplo1);
$exemplos->appendChild($exemplo2);
$exemplos->appendChild($exemplo3);

$root->appendChild($exemplos);

echo $dom->saveXML($root, LIBXML_NOEMPTYTAG);

Output:

<root><exemplo><exemplo></exemplo><exemplo></exemplo><exemplo></exemplo></exemplo></root>

Example: use sample.

    
05.09.2016 / 21:19