note 84660 added to domdocument.createattribute
| From: | chandrachur at elegantsystems dot net | Date: | Thu, 24 Jul 2008 07:41:38 +0000 |
| Subject: | note 84660 added to domdocument.createattribute | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-142637@lists.php.net to get a copy of this message | ||
A pretty simple example showing how to create attributes and add values to them:
$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>
----
Server IP: 203.199.69.133
Probable Submitter: 59.97.176.62
----
Manual Page -- http://www.php.net/manual/en/domdocument.createattribute.php
Edit -- https://master.php.net/note/edit/84660
Del: integrated -- https://master.php.net/note/delete/84660/integrated
Del: useless -- https://master.php.net/note/delete/84660/useless
Del: bad code -- https://master.php.net/note/delete/84660/bad+code
Del: spam -- https://master.php.net/note/delete/84660/spam
Del: non-english -- https://master.php.net/note/delete/84660/non-english
Del: in docs -- https://master.php.net/note/delete/84660/in+docs
Del: other reasons-- https://master.php.net/note/delete/84660
Reject -- https://master.php.net/note/reject/84660
Search -- https://master.php.net/manage/user-notes.php