Home > Software engineering >  php create nested xml
php create nested xml

Time:10-19

i have this php code to create xml.

  <?php 
    $dom = new DOMDocument();
    $document = $dom->appendChild(
      $dom->createElement('xtranslation') 
    );
    $document->textContent = '21st Century King James Version';
    
    $document->appendChild($dom->createElement('xabbreviation'))->textContent = 'KJ21';

    $dom->formatOutput = true;
    echo $dom->saveXml();
    ?>

but it create inline xml code like below.

<?xml version="1.0"?>
<xtranslation>21st Century King James Version<xabbreviation>KJ21</xabbreviation></xtranslation>

how can i get proper structure like below?

<?xml version="1.0"?>
<xtranslation>21st Century King James Version
  <xabbreviation>KJ21</xabbreviation>
</xtranslation>

It works properly if i remove

$document->textContent = '21st Century King James Version';

CodePudding user response:

problem was in displaying data. when i add

header("Content-type: text/xml");

it show xml data properly

  • Related