PHP | DOMDocument createAttribute()函数
DOMDocument::createAttribute()函数是PHP中的一个内置函数,用于创建 DOMAttr 类的新实例。
句法:
DOMAttr DOMDocument::createAttribute( string $name )
参数:此函数接受单个参数$name ,其中包含属性的名称。
返回值:此函数在成功时返回新的 DOMAttr 对象,在失败时返回 FALSE。
下面的程序说明了PHP中的 DOMDocument::createAttribute()函数:
方案一:
createElement('organization',
'A computer science portal');
// Use createAttribute() function to create an attribute node
$domAttribute = $domDocument->createAttribute('name');
// Value of created attribute
$domAttribute->value = 'GeeksforGeeks';
// Append element to the document
$domElement->appendChild($domAttribute);
// Append it to the document itself
$domDocument->appendChild($domElement);
// Save the document into XML and display it
echo $domDocument->saveXML();
?>
输出:
A computer science portal
方案二:
createElement('organization',
'GeeksforGeeks');
$domAttribute1 = $domDocument->createAttribute('name');
// Value for the created attribute
$domAttribute1->value = 'GeeksforGeeks';
// Append element to the document
$domElement->appendChild($domAttribute1);
// Use createAttribute() function to create an attribute node
$domAttribute2 = $domDocument->createAttribute('address');
// Value for the created attribute
$domAttribute2->value = 'Noida';
// Append element to the document
$domElement->appendChild($domAttribute2);
// Use createAttribute() function to create an attribute node
$domAttribute3 = $domDocument->createAttribute('email');
// Value for the created attribute
$domAttribute3->value = 'abc@geeksforgeeks.org';
// Append element to the document
$domElement->appendChild($domAttribute3);
// Append it to the document itself
$domDocument->appendChild($domElement);
// Save the document to XML and display it
echo $domDocument->saveXML();
?>
输出:
GeeksforGeeks
参考: https://www. PHP.net/manual/en/domdocument.createattribute。 PHP