【问题标题】:DOMDocument cannot change parentNodeDOMDocument 无法更改父节点
【发布时间】:2015-05-29 11:48:31
【问题描述】:

我无法将 DOMDocument parentNode 从 null 更改。我尝试过同时使用appendChildreplaceChild,但没有任何运气。

我哪里错了?

   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

【问题讨论】:

  • 你不会从 esle 块返回任何东西
  • 我找不到变量parentNode ...
  • 在 else 块之后移动 echo $DOMDocument-&gt;saveXML();
  • 有点混乱...你能显示预期的输出吗?
  • @Michael Wilson 我试试你的链接,我是对的 - 移动后它可以工作

标签: php php-5.5


【解决方案1】:

由于文档没有 parent 节点,您需要将根节点直接附加到文档中,如下所示:

$DOMDocument->appendChild($node);

DOMDocument 扩展 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);

顺便说一句,如果您只想序列化 XML 文件,DOM 有点开销。我会为此使用模板引擎,这意味着将其作为纯文本处理。

【讨论】:

    【解决方案2】:

    这应该可以,当你创建一个新的 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);
            }
        }
    }
    

    我不确定你为什么需要 parentNode?你可以做$DOMDocument-&gt;appendChild();

    【讨论】:

      猜你喜欢
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 2011-07-28
      相关资源
      最近更新 更多