php - DOMDocument cannot change parentNode

222

I cannot change theDOMDocumentparentNode from null. I have tried using bothappendChild andreplaceChild, but haven't had any luck.

Where am I going 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

899

Answer

Solution:

Since the document has no parent node you need to append the root node directly to the document, like this:

$DOMDocument->appendChild($node);

This works sinceDOMDocument extendsDOMNode.


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,DOM is a bit overhead. I would use a template engine for this, meaning handle it as plain text.

587

Answer

Solution:

This should work, when you create a new DOMDocument you don't have a root element yet, so you can just create it and add it to the document

//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);
        }
    }
}

I'm not sure why you need the parentNode? you could do$DOMDocument->appendChild();

People are also looking for solutions to the problem: php - Google Calendar API Time

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.