首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DOMDocument无法更改parentNode

DOMDocument无法更改parentNode
EN

Stack Overflow用户
提问于 2015-05-29 11:48:31
回答 2查看 571关注 0票数 4

我不能将DOMDocument parentNode更改为null。我试过使用appendChildreplaceChild,但没有任何运气。

我哪里出问题了?

代码语言:javascript
复制
   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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-01 12:10:26

由于文档没有父节点,所以需要直接将根节点追加到文档中,如下所示:

代码语言:javascript
复制
$DOMDocument->appendChild($node);

这是因为DOMDocument扩展了DOMNode

工作实例

代码语言:javascript
复制
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就有点开销。为此,我将使用模板引擎,意思是将其处理为纯文本。

票数 3
EN

Stack Overflow用户

发布于 2015-06-01 12:40:28

这应该是可行的,当您创建一个新的DOMDocument时,您还没有根元素,所以您可以只创建它并将它添加到文档中

代码语言:javascript
复制
//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->appendChild();

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30528839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档