DOMDocument::createAttribute
(PHP 5, PHP 7, PHP 8)
DOMDocument::createAttribute — Create new attribute
说明
This function creates a new instance of class DOMAttr. 此节点出现在文档中,除非是用诸如 DOMNode->appendChild() 等函数来将其插入。
参数
localName
-
The name of the attribute.
参见
- DOMNode::appendChild() - Adds new child at the end of the children
- DOMDocument::createAttributeNS() - Create new attribute node with an associated namespace
- DOMDocument::createCDATASection() - Create new cdata node
- DOMDocument::createComment() - Create new comment node
- DOMDocument::createDocumentFragment() - Create new document fragment
- DOMDocument::createElement() - Create new element node
- DOMDocument::createElementNS() - Create new element node with an associated namespace
- DOMDocument::createEntityReference() - Create new entity reference node
- DOMDocument::createProcessingInstruction() - Creates new PI node
- DOMDocument::createTextNode() - Create new text node
+添加备注
用户贡献的备注 1 note
Beerkeeper ¶
13 years ago
Just in case it isn't clear (like I had), an example:
<?php
$domDocument = new DOMDocument('1.0', "UTF-8");
$domElement = $domDocument->createElement('field','some random data');
$domAttribute = $domDocument->createAttribute('name');
// Value for the created attribute
$domAttribute->value = 'attributevalue';
// Don't forget to append it to the element
$domElement->appendChild($domAttribute);
// Append it to the document itself
$domDocument->appendChild($domElement);
?>
Will output:
<?xml version="1.0" encoding="UTF-8"?>
<field name="attributevalue">some random data</field>
备份地址:http://www.lvesu.com/blog/php/domdocument.createattribute.php