在php中调用未定义的方法DOMElement :: appendChid()

时间:2022-01-18 23:34:31

Following is the working code that is generating this XML -

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

Working code link - http://codepad.org/aX5HL6Vp

工作代码链接 - http://codepad.org/aX5HL6Vp

    $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 generated from the code -

从代码生成的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>

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

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

XML to generate -

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>

Code NOT working -

代码不工作 -

Codepad link - http://codepad.org/XLwp4AbQ

键盘链接 - http://codepad.org/XLwp4AbQ

    $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());

Error -

错误 -

Fatal error: Call to undefined method DOMElement::appendChid()

致命错误:调用未定义的方法DOMElement :: appendChid()

Let me know what I am doing wrong ?

让我知道我做错了什么?

1 个解决方案

#1


3  

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

你拼错了,而不是appendChild你写了appendChid。

First correct it, and then check what happens.

首先纠正它,然后检查发生了什么。

#1


3  

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

你拼错了,而不是appendChild你写了appendChid。

First correct it, and then check what happens.

首先纠正它,然后检查发生了什么。