A pretty simple example showing how to create attributes and add values to them:
<?php
$doc = new DOMDocument('1.0', 'UTF-8');
$root = $doc->createElement('songs');
$doc->appendChild($root);
for($i=0;$i<10;$i++){
$root_child = $doc->createElement('song');
$root->appendChild($root_child);
$root_attr1 = $doc->createAttribute('url');
$root_child->appendChild($root_attr1);
$root_text = $doc->createTextNode('This is the root element!');
$root_attr1->appendChild($root_text);
$root_attr2= $doc->createAttribute('artist');
$root_child->appendChild($root_attr2);
$root_text = $doc->createTextNode('This is the root element!');
$root_attr2->appendChild($root_text);
$root_attr3 = $doc->createAttribute('track');
$root_child->appendChild($root_attr3);
$root_text = $doc->createTextNode('This is the root element!');
$root_attr3->appendChild($root_text);
}
print $doc->saveXML();
?>
This will output as:
<?xml version="1.0" encoding="UTF-8" ?>
<songs>
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
<song url="This is the root element!" artist="This is the root element!" track="This is the root element!" />
</songs>
DOMDocument::createAttribute
(PHP 5)
DOMDocument::createAttribute — 新しい属性を作成する
説明
この関数は、DOMAttr クラスの新しいインスタンスを作成します。 このノードは、(たとえば) DOMNode->appendChild() などで 挿入されない限り、ドキュメント内に現われません。
パラメータ
- name
-
属性の名前。
返り値
新しい DOMAttr、あるいはエラーが発生した場合は FALSE を返します。
エラー / 例外
- DOM_INVALID_CHARACTER_ERR
-
name が無効な文字を含んでいる場合に発生します。
参考
- DOMNode::appendChild - 子要素群の最後に新しい子要素を追加する
- DOMDocument::createAttributeNS - 関連付けられた名前空間に新しい属性を作成する
- DOMDocument::createCDATASection - 新しい cdata ノードを作成する
- DOMDocument::createComment - 新しい comment ノードを作成する
- DOMDocument::createDocumentFragment - 新しい文書片を作成する
- DOMDocument::createElement - 新しい要素ノードを作成する
- DOMDocument::createElementNS - 関連付けられた名前空間に新しい要素を作成する
- DOMDocument::createEntityReference - 新しいエンティティ参照ノードを作成する
- DOMDocument::createProcessingInstruction - 新しい PI ノードを作成する
- DOMDocument::createTextNode - 新しいテキストノードを作成する
DOMDocument::createAttribute
chandrachur at elegantsystems dot net
24-Jul-2008 04:41
24-Jul-2008 04:41
boen dot robot at the-google-mail dot com
13-Nov-2006 03:43
13-Nov-2006 03:43
If you're looking for an easy way to create an attribute with a certain value, in a similar to createElement() manner, you should use the setAttribute() instead. Documentation and example is available below:
http://php.net/manual/en/domdocument.setattribute.php
