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

时间:2013-09-04 10:19:24

标签: php xml xml-parsing domdocument

以下是生成此XML的working代码 -

工作代码链接 - 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 -

<?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节点,一切都出错了:(

要生成的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>

代码工作 -

键盘链接 - 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());

错误 -

  

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

让我知道我做错了什么?

1 个答案:

答案 0 :(得分:3)

你写了一个拼写错误,而不是appendChild你写appendChid

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