DOMDocument cannot change parentNode

I can not change DOMDocument parentNodefrom null. I tried using appendChildand replaceChildbut had no luck.

Where am I wrong here?   

   error_reporting(E_ALL);

   function xml_encode($mixed, $DOMDocument=null) {
      if (is_null($DOMDocument)) {
          $DOMDocument =new DOMDocument;
          $DOMDocument->formatOutput = true;
          xml_encode($mixed, $DOMDocument);
          echo $DOMDocument->saveXML();
      } else {
          if (is_array($mixed)) {
              $node = $DOMDocument->createElement('urlset', 'hello');
              $DOMDocument->parentNode->appendChild($node);
          }
      }
  }

  $data = array();

  for ($x = 0; $x <= 10; $x++) {
      $data['urlset'][] = array(
         'loc' => 'http://www.example.com/user',
         'lastmod' => 'YYYY-MM-DD',
         'changefreq' => 'monthly',
         'priority' => 0.5
      );
  }

  header('Content-Type: application/xml');
  echo xml_encode($data);

?>

http://runnable.com/VWhQksAhdIJYEPLj/xml-encode-for-php

+4
source share
2 answers

Since the document does not have a parent node, you need to add the root directory of the node directly to the document, for example:

$DOMDocument->appendChild($node);

This works with DOMDocumentextends DOMNode.


Working example :

error_reporting(E_ALL);

function xml_encode($mixed, &$DOMDocument=null) {
    if (is_null($DOMDocument)) {
        $DOMDocument =new DOMDocument;
        $DOMDocument->formatOutput = true;
        xml_encode($mixed, $DOMDocument);
        return $DOMDocument->saveXML();
    } else {
        if (is_array($mixed)) {
            $node = $DOMDocument->createElement('urlset', 'hello');
            $DOMDocument->appendChild($node);
        }
    }   
}

$data = array();
for ($x = 0; $x <= 10; $x++) {
    $data['urlset'][] = array(
       'loc' => 'http://www.example.com/user',
       'lastmod' => 'YYYY-MM-DD',
       'changefreq' => 'monthly',
       'priority' => 0.5 
    );  
}

header('Content-Type: application/xml');
echo xml_encode($data);

Btw, if you just want to serialize an XML file, a DOMlittle overhead. I would use a template engine for this, meaning to treat it like plain text.

+3
source

, DOMDocument, ,

//You could add this to the top of xml_encode
if($DOMDocument->parentNode === null) {
   $root = $DOMDocument->createElement('root');
   $root = $DOMDocument->appendChild($root);
}

//Your script working: 
<?php
error_reporting(E_ALL);

function xml_encode($mixed, $DOMDocument=null) {
    if (is_null($DOMDocument)) {
        $DOMDocument =new DOMDocument();
        $DOMDocument->formatOutput = true;

        //add here, but note that in the "else" it isn't sure if the DOMDocument has a root element 
        $root = $DOMDocument->createElement('root');
        $root = $DOMDocument->appendChild($root);
        xml_encode($mixed, $root);

        echo $DOMDocument->saveXML();
    } else {
        if (is_array($mixed)) {
            $node = $DOMDocument->createElement('urlset', 'hello');
            $DOMDocument->parentNode->appendChild($node);
        }
    }
}

, ? $DOMDocument->appendChild();

+1

All Articles