Call to undefined method DOMElement::appendChid()

2019-09-20 14:26发布

Following is the working code that is generating this XML -

Working code link - 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 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 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

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

Let me know what I am doing wrong ?

1条回答
劫难
2楼-- · 2019-09-20 15:17

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

First correct it, and then check what happens.

查看更多
登录 后发表回答