Skip to content

Instantly share code, notes, and snippets.

@felds
Created October 15, 2009 03:40
Show Gist options
  • Save felds/210646 to your computer and use it in GitHub Desktop.
Save felds/210646 to your computer and use it in GitHub Desktop.
Instructions on using DomDocument class on PHP 5
<?php
// Setting the document header
header('content-type: application/xml; charset=UTF-8');
// Instantiating a new DomDoc obj.
$xml = new DomDocument('1.0','UTF-8');
$xml->formatOutput = true;
// Creating a root element and assigning it to $xmlRoot variable
$xml->appendChild($xmlRoot = $xml->createElement('root'));
// Populating the root element with an empty tag
$xmlRoot->appendChild($xml->createElement('emptyTag'));
// ... with a tag with attributes and a child
$xmlRoot->appendChild($xmlRootTagWithAtt = $xml->createElement('tagWithAtt'));
$xmlRootTagWithAtt->setAttribute('att', 'No need 4 escapes &éàä. See? :)');
$xmlRootTagWithAtt->appendChild($xml->createElement('anotherEmptyTag'));
// ... and with a tag containing a text node
$xmlRoot->appendChild($xml->createElement('tagWithText', 'Ni!'));
// Printing out.
echo $xml->saveXML();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment