DOMDocument::createAttribute()函数是PHP中的内置函数,用于创建类DOMAttr的新实例。
用法:
DOMAttr DOMDocument::createAttribute( string $name )
参数:该函数接受单个参数$name,该参数保存属性的名称。
返回值:成功时此函数返回新的DOMAttr对象,失败时返回FALSE。
以下示例程序旨在说明PHP中的DOMDocument::createAttribute()函数:
程序1:
<?php
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
// Use createElement() function to create an element node
$domElement = $domDocument->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();
?>
输出:
<?xml version="1.0" encoding="iso-8859-1"?> <organization name="GeeksforGeeks">A computer science portal</organization>
程序2:
<?php
// Create a new DOMDocument
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
// Use createElement() function to create an element node
$domElement = $domDocument->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();
?>
输出:
<?xml version="1.0" encoding="iso-8859-1"?> <organization name="GeeksforGeeks" address="Noida" email="abc@geeksforgeeks.org"> GeeksforGeeks </organization>
参考: https://www.php.net/manual/en/domdocument.createattribute.php
相关用法
- PHP DOMDocument createElement()用法及代码示例
- PHP DOMDocument createAttributeNS()用法及代码示例
- PHP DOMDocument createComment()用法及代码示例
- PHP DOMDocument createElementNS()用法及代码示例
- PHP DOMDocument normalizeDocument()用法及代码示例
- PHP DOMDocument loadHTMLFile()用法及代码示例
- PHP DOMDocument loadHTML()用法及代码示例
- PHP DOMDocument createCDATASection()用法及代码示例
- PHP DOMDocument load()用法及代码示例
- PHP DOMDocument getElementsByTagName()用法及代码示例
- PHP DOMDocument createProcessingInstruction()用法及代码示例
- PHP DOMDocument importNode()用法及代码示例
- PHP DOMDocument createTextNode()用法及代码示例
- PHP DOMDocument saveXML()用法及代码示例
- PHP DOMDocument __construct()用法及代码示例
注:本文由纯净天空筛选整理自jit_t大神的英文原创作品 PHP | DOMDocument createAttribute() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。