Created
October 15, 2009 03:40
-
-
Save felds/210646 to your computer and use it in GitHub Desktop.
Instructions on using DomDocument class on PHP 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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