Creating XML with DOM

Creating XML file with dom is very easy and is very hand while creating sitemap or feed for our site.

<?php
$dom = new domDocument('1.0');
$dom->formatOutput = true; // make output be nicely formatted
$root = $dom->createElement('root'); // create new element
foreach (array('foo', 'bar', 'baz') as $v) {
    $node = $dom->createElement($v); // create new sub-element
    $node->appendChild($dom->createTextNode($v)); // add value to element
    $root->appendChild($node); // append sub-element to root element
}
$dom->appendChild($root); // add root node to document
$xml = $dom->saveXML(); // output XML

echo nl2br(htmlspecialchars($xml));
?>

Output

<?xml version="1.0"?>
<root>
<foo>foo</foo>
<bar>bar</bar>
<baz>baz</baz>
</root>

 


Loading ...

Related Results :

  1. PHP SimpleXML
  2. Creating XML with DOM
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author