note 95229 added to class.simplexmlelement

From: Date: Sun, 20 Dec 2009 00:19:44 +0000
Subject: note 95229 added to class.simplexmlelement
Groups: php.notes 
Request: Send a blank email to php-notes+get-164145@lists.php.net to get a copy of this message
It's occasionally useful to add an XML processing instruction to a SimpleXMLElement (treating it as if it were a full document). <?php class SimpleXMLElement_Plus extends SimpleXMLElement { public function addProcessingInstruction( $name, $value ) { // Create a DomElement from this simpleXML object $dom_sxe = dom_import_simplexml($this); // Create a handle to the owner doc of this xml $dom_parent = $dom_sxe->ownerDocument; // Find the topmost element of the domDocument $xpath = new DOMXPath($dom_parent); $first_element = $xpath->evaluate('/*[1]')->item(0); // Add the processing instruction before the topmost element $pi = $dom_parent->createProcessingInstruction($name, $value); $dom_parent->insertBefore($pi, $first_element); } } ?> For example, if you had a simpleXMLElemnt_Plus object made out of the xml fragment: <xml><content /></xml> And you needed the output to be: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="xsl/xsl.xsl"?> <xml><content/></xml> you could do (using the class above): $xml = new SimpleXMLElement_Plus('<xml><content /></xml>'); $xml->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="xsl/xsl.xsl"'); echo $xml->asXML(); ---- Server IP: 208.69.120.55 Probable Submitter: 71.81.35.228 ---- Manual Page -- http://www.php.net/manual/en/class.simplexmlelement.php Edit -- https://master.php.net/note/edit/95229 Del: integrated -- https://master.php.net/note/delete/95229/integrated Del: useless -- https://master.php.net/note/delete/95229/useless Del: bad code -- https://master.php.net/note/delete/95229/bad+code Del: spam -- https://master.php.net/note/delete/95229/spam Del: non-english -- https://master.php.net/note/delete/95229/non-english Del: in docs -- https://master.php.net/note/delete/95229/in+docs Del: other reasons-- https://master.php.net/note/delete/95229 Reject -- https://master.php.net/note/reject/95229 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#164145) next »