DOMDocument::createAttribute

(PHP 5, PHP 7, PHP 8)

DOMDocument::createAttribute โ€” ๆ–ฐใ—ใ„ๅฑžๆ€งใ‚’ไฝœๆˆใ™ใ‚‹

่ชฌๆ˜Ž

public function DOMDocument::createAttribute(string $localName): DOMAttr|false

ใ“ใฎ้–ขๆ•ฐใฏใ€DOMAttr ใ‚ฏใƒฉใ‚นใฎๆ–ฐใ—ใ„ใ‚คใƒณใ‚นใ‚ฟใƒณใ‚นใ‚’ไฝœๆˆใ—ใพใ™ใ€‚ ใ“ใฎใƒŽใƒผใƒ‰ใฏใ€( DOMNode::appendChild() ใชใฉใง) ๆŒฟๅ…ฅใ•ใ‚Œใชใ„้™ใ‚Šใ€ใƒ‰ใ‚ญใƒฅใƒกใƒณใƒˆๅ†…ใซใ‚ใ‚‰ใ‚ใ‚Œใพใ›ใ‚“ใ€‚

ใƒ‘ใƒฉใƒกใƒผใ‚ฟ

localName

ๅฑžๆ€งใฎๅๅ‰ใ€‚

ๆˆปใ‚Šๅ€ค

ๆ–ฐใ—ใ„ DOMAttrใ€ใ‚ใ‚‹ใ„ใฏใ‚จใƒฉใƒผใŒ็™บ็”Ÿใ—ใŸๅ ดๅˆใฏ false ใ‚’่ฟ”ใ—ใพใ™ใ€‚

ใ‚จใƒฉใƒผ / ไพ‹ๅค–

ไปฅไธ‹ใฎใ‚จใƒฉใƒผใ‚ณใƒผใƒ‰ใง DOMException ใ‚’ใ‚นใƒญใƒผใ—ใพใ™:

DOM_INVALID_CHARACTER_ERR

localName ใŒ็„กๅŠนใชๆ–‡ๅญ—ใ‚’ๅซใ‚“ใงใ„ใ‚‹ๅ ดๅˆใซ็™บ็”Ÿใ—ใพใ™ใ€‚

ๅ‚่€ƒ

๏ผ‹add a note

User Contributed Notes 1 note

up
88
Beerkeeper ยถ
14 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>