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
(No version information available, might be only in CVS)
DOMDocument::createAttribute — Create new attribute
Descrizione
This function creates a new instance of class DOMAttr. Questo nodo non verrà mostrato nel documento a meno che venga inserito con, es. DOMNode->appendChild().
Elenco dei parametri
- name
-
The name of the attribute.
Valori restituiti
The new DOMAttr or FALSE if an error occured.
Errori/Eccezioni
- DOM_INVALID_CHARACTER_ERR
-
Raised if name contains an invalid character.
Vedere anche:
- DOMNode::appendChild
- DOMDocument::createAttributeNS
- DOMDocument::createCDATASection
- DOMDocument::createComment
- DOMDocument::createDocumentFragment
- DOMDocument::createElement
- DOMDocument::createElementNS
- DOMDocument::createEntityReference
- DOMDocument::createProcessingInstruction
- DOMDocument::createTextNode
DOMDocument::createAttribute
chandrachur at elegantsystems dot net
24-Jul-2008 09:41
24-Jul-2008 09:41
