1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?php
// Créer un nouveau document
$doc = new DOMDocument;
// Créer un nouveau noeud 'root'
$root = $doc->appendChild(new DOMElement('root'));
// Créer un nouvau noeud 'articles'
$articles = $root->appendChild(new DOMElement('articles'));
for ($i=0; $i<5; $i++) {
// Ajouter un article au noeud 'articles'
$article = $articles->appendChild(new DOMElement('article', 'Lorem ipsum sit amet...'));
// Définir l'attribut 'date' du noeud 'article courant
$article->setAttribute('date', date('d/m/Y', strtotime("-$i days", time())));
}
// Ajouter un attribut aux dernier article
$article->setAttribute('last', 'last');
// Envoyer un header XML pour le navigateur
header('Content-Type: text/xml');
// Afficher le document
echo $doc->saveXML(); |
Partager