PHP DOMElement是不可变的。 ='没有修改允许错误'

时间:2022-11-20 20:29:04

I cannot understand why this fails. Does a DOMElement need to be part of a Document?

我无法理解为什么会失败。 DOMElement是否需要成为文档的一部分?

$domEl = new DOMElement("Item"); 
$domEl->setAttribute('Something','bla'); 

Throws exception

引发异常

> Uncaught exception 'DOMException' with message 'No Modification Allowed Error';

I would have thought I could just create a DOMElement and it would be mutable.

我原本以为我可以创建一个DOMElement,它会变得可变。

1 个解决方案

#1


30  

From http://php.net/manual/en/domelement.construct.php

来自http://php.net/manual/en/domelement.construct.php

Creates a new DOMElement object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writeable node, use DOMDocument::createElement or DOMDocument::createElementNS.

创建一个新的DOMElement对象。该对象是只读的。它可以附加到文档,但是在节点与文档相关联之前,可以不将附加节点附加到该节点。要创建可写节点,请使用DOMDocument :: createElement或DOMDocument :: createElementNS。

#1


30  

From http://php.net/manual/en/domelement.construct.php

来自http://php.net/manual/en/domelement.construct.php

Creates a new DOMElement object. This object is read only. It may be appended to a document, but additional nodes may not be appended to this node until the node is associated with a document. To create a writeable node, use DOMDocument::createElement or DOMDocument::createElementNS.

创建一个新的DOMElement对象。该对象是只读的。它可以附加到文档,但是在节点与文档相关联之前,可以不将附加节点附加到该节点。要创建可写节点,请使用DOMDocument :: createElement或DOMDocument :: createElementNS。