note 94718 added to class.simplexmlelement

From: Date: Fri, 20 Nov 2009 02:55:13 +0000
Subject: note 94718 added to class.simplexmlelement
Groups: php.notes 
Request: Send a blank email to php-notes+get-163119@lists.php.net to get a copy of this message
Hello, here goes my contribution for those whom are struggling to understand how SimpleXMLElement works. After some time trying to figure out how this works, I've came up to this small example: <?php $xmlstr = "<?xml version='1.0' ?>\n". // optionally you can specify a xml-stylesheet for presenting the results. just uncoment the following line and change the stylesheet name. /* "<?xml-stylesheet type='text/xsl' href='xml_style.xsl' ?>\n". */ "<book></book>"; // create the SimpleXMLElement object with an empty <book> element $xml = new SimpleXMLElement($xmlstr); // add some child nodes $xml->addChild("title", "Title of my book"); $xml->addChild("abstract", "My book is about learning to work with SimpleXMLElement"); // add some more child nodes $chapter1 = $xml->addChild("chapter_1"); // add an attribute to child chapter_1 $chapter1->addAttribute("chapter_title", "Introduction to my book"); $chapter2 = $xml->addChild("chapter_2"); $chapter2->addAttribute("chapter_title", "Development of my book"); $chapter3 = $xml->addChild("chapter_3"); $chapter3->addAttribute("chapter_title", "Another chapter of my book"); $conclusion = $xml->addChild("conclusion", "The ending of my book"); // insert the header to tell the browser how to read the document header("Content-type: text/xml"); // print the SimpleXMLElement as a XML well-formed string echo $xml->asXML(); ?> With this script you can just copy-paste and try to understand how it works. I hope it can help somebody :) ---- Server IP: 194.8.30.16 Probable Submitter: 194.117.22.135 ---- Manual Page -- http://www.php.net/manual/en/class.simplexmlelement.php Edit -- https://master.php.net/note/edit/94718 Del: integrated -- https://master.php.net/note/delete/94718/integrated Del: useless -- https://master.php.net/note/delete/94718/useless Del: bad code -- https://master.php.net/note/delete/94718/bad+code Del: spam -- https://master.php.net/note/delete/94718/spam Del: non-english -- https://master.php.net/note/delete/94718/non-english Del: in docs -- https://master.php.net/note/delete/94718/in+docs Del: other reasons-- https://master.php.net/note/delete/94718 Reject -- https://master.php.net/note/reject/94718 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#163119) next »