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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。