note 82447 added to class.domdocument

From: Date: Fri, 11 Apr 2008 07:48:01 +0000
Subject: note 82447 added to class.domdocument
Groups: php.notes 
Request: Send a blank email to php-notes+get-138177@lists.php.net to get a copy of this message
Showing a quick example of how to use this class, just so that new users can get a quick start without having to figure it all out by themself. ( At the day of posting, this documentation just got added and is lacking examples. ) <?php // Set the content type to be XML, so that the browser will recognise it as XML. header( "content-type: application/xml; charset=ISO-8859-15" ); // "Create" the document. $xml = new DOMDocument( "1.0", "ISO-8859-15" ); // Create some elements. $xml_album = $xml->createElement( "Album" ); $xml_track = $xml->createElement( "Track", "The ninth symphony" ); // Set the attributes. $xml_track->setAttribute( "length", "0:01:15" ); $xml_track->setAttribute( "bitrate", "64kb/s" ); $xml_track->setAttribute( "channels", "2" ); // Create another element, just to show you can add any (realistic to computer) number of sublevels. $xml_note = $xml->createElement( "Note", "The last symphony composed by Ludwig van Beethoven." ); // Append the whole bunch. $xml_track->appendChild( $xml_note ); $xml_album->appendChild( $xml_track ); // Repeat the above with some different values.. $xml_track = $xml->createElement( "Track", "Highway Blues" ); $xml_track->setAttribute( "length", "0:01:33" ); $xml_track->setAttribute( "bitrate", "64kb/s" ); $xml_track->setAttribute( "channels", "2" ); $xml_album->appendChild( $xml_track ); $xml->appendChild( $xml_album ); // Parse the XML. print $xml->saveXML(); ?> Output: <Album> <Track length="0:01:15" bitrate="64kb/s" channels="2"> The ninth symphony <Note> The last symphony composed by Ludwig van Beethoven. </Note> </Track> <Track length="0:01:33" bitrate="64kb/s" channels="2">Highway Blues</Track> </Album> If you want your PHP->DOM code to run under the .xml extension, you should set your webserver up to run the .xml extension with PHP ( Refer to the installation/configuration configuration for PHP on how to do this ). Note that this: <?php $xml = new DOMDocument( "1.0", "ISO-8859-15" ); $xml_album = $xml->createElement( "Album" ); $xml_track = $xml->createElement( "Track" ); $xml_album->appendChild( $xml_track ); $xml->appendChild( $xml_album ); ?> is NOT the same as this: <?php // Will NOT work. $xml = new DOMDocument( "1.0", "ISO-8859-15" ); $xml_album = new DOMElement( "Album" ); $xml_track = new DOMElement( "Track" ); $xml_album->appendChild( $xml_track ); $xml->appendChild( $xml_album ); ?> although this will work: <?php $xml = new DOMDocument( "1.0", "ISO-8859-15" ); $xml_album = new DOMElement( "Album" ); $xml->appendChild( $xml_album ); ?> ---- Server IP: 213.249.64.162 Probable Submitter: 83.98.252.88 ---- Manual Page -- http://www.php.net/manual/en/class.domdocument.php Edit -- https://master.php.net/note/edit/82447 Del: integrated -- https://master.php.net/note/delete/82447/integrated Del: useless -- https://master.php.net/note/delete/82447/useless Del: bad code -- https://master.php.net/note/delete/82447/bad+code Del: spam -- https://master.php.net/note/delete/82447/spam Del: non-english -- https://master.php.net/note/delete/82447/non-english Del: in docs -- https://master.php.net/note/delete/82447/in+docs Del: other reasons-- https://master.php.net/note/delete/82447 Reject -- https://master.php.net/note/reject/82447 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138177) next »