本文介绍了在PHP中调用未定义的方法DOMElement :: appendChid()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是正在生成XML的工作代码-

Following is the working code that is generating this XML -

工作代码链接-

    $dom = new DOMDocument('1.0');
    $dom->xmlStandalone = false;
    $manfiestNode = $dom->createElement('manifest',"");
    $manfiestNodeAttr = $dom->createAttribute('identifier');
    $date = new DateTime();
    $manfiestNodeAttr->value = 'course_'.date_format($date,'U');
    $manfiestNode->appendChild($manfiestNodeAttr);
$manfiestNode->appendChild($dom->createAttribute('xmlns:xsi'))->appendChild($dom->createTextNode("http://www.w3.org/2001/XMLSchema-instance"));
    $metaData = $dom->createElement('metadata','');
    $manfiestNode->appendChild($metaData);
    $dom->appendChild($manfiestNode);
    var_dump($dom->saveXML());

从代码生成的XML-

<?xml version="1.0" standalone="no" ?>
<manifest identifier="com.scorm.golfsamples.contentpackaging.multioscosinglefile.20043rd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <metadata>
  </metadata>
</manifest>

但是我试图将子节点添加到 metadata 节点,一切都出错了:(

But I am trying to add child node to metadata node and everything went wrong :(

XML生成-

<?xml version="1.0" standalone="no" ?>
<manifest identifier="com.scorm.golfsamples.contentpackaging.multioscosinglefile.20043rd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <metadata>
    <schema>ADL SCORM</schema>
    <schemaversion>2004 3rd Edition</schemaversion>
  </metadata>
</manifest>

代码有效-

键盘链接-

    $dom = new DOMDocument('1.0');
    $dom->xmlStandalone = false;   
    $manfiestNode = $dom->createElement('manifest',"");
    $manfiestNodeAttr = $dom->createAttribute('identifier');
    $date = new DateTime();
    $manfiestNodeAttr->value = 'course_'.date_format($date,'U');
    $manfiestNode->appendChild($manfiestNodeAttr);
$manfiestNode->appendChild($dom->createAttribute('xmlns:xsi'))->appendChild($dom->createTextNode("http://www.w3.org/2001/XMLSchema-instance"));
    $metaData = $dom->createElement('metadata','');
    $manfiestNode->appendChild($metaData);
    $schema = $dom->createElement('schema','ADL SCORM');
    $schemaVersion = $dom->createElement('schemaversion', '2004 3rd Edition');
    $metaData->appendChid($schema);
    $metaData->appendChid($schemaVersion);
    $dom->appendChild($manfiestNode);
    var_dump($dom->saveXML());

错误-

让我知道我做错了吗?

Let me know what I am doing wrong ?

推荐答案

您犯了一个拼写错误,而不是您编写的 appendChild appendChid

You have made a spelling mistake, instead of appendChild you write appendChid.

首先更正它,然后检查会发生什么。

First correct it, and then check what happens.

这篇关于在PHP中调用未定义的方法DOMElement :: appendChid()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 12:52