牛牛在线工具

DOMElement::__construct()

收录 8868 个 PHP 函数,含用途说明与用法示例。

DOMElement::__construct()

创建 DOMElement 对象,并且可以设置其元素的名称和命名空间

函数: DOMElement::__construct() 适用版本: PHP 5, PHP 7 用法: DOMElement::__construct() 函数用于创建 DOMElement 对象,并且可以设置其元素的名称和命名空间。 语法: public DOMElement::__construct ( string $qualifiedName [, string $value [, string $namespaceURI ]] ) 参数: $qualifiedName:表示要创建的元素的名称。必选参数。 $value:表示要为元素设置的值。可选参数。 $namespaceURI:表示元素的命名空间URI。可选参数。 示例: <?php // 创建一个新的 <book> 元素 $dom = new DOMDocument(); $book = $dom->createElement("book"); $dom->appendChild($book); // 创建一个带有命名空间的 <title> 元素 $title = $dom->createElementNS("http://www.example.com/namespace", "title", "PHP Basics"); $book->appendChild($title); // 创建一个带有值的 <author> 元素 $author = new DOMElement("author", "John Doe"); $book->appendChild($author); echo $dom->saveXML(); ?> 输出: <book> <title xmlns="http://www.example.com/namespace">PHP Basics</title> <author>John Doe</author> </book> 在上述示例中,我们首先创建了一个 DOMElement 对象 $book,然后通过 appendChild() 方法将其添加到 DOMDocument 对象 $dom 中。接下来,我们使用了 createElementNS() 方法创建了一个带有命名空间的 DOMElement 对象 $title,并将其作为子元素添加到 $book 中。最后,使用 DOMElement 的构造函数创建了一个带有值的 DOMElement 对象 $author,并将其作为子元素添加到 $book 中。最后,使用 saveXML() 方法将生成的 XML 输出。