Re: [PHP] Re: Using DOM object, how?
On Sep 19, 2005, at 7:19 PM, Stephen Leaf wrote: On Monday 19 September 2005 06:04 pm, Ken Tozier wrote: Not a bad Idea. You might like this function I made then ;) function createElement($parentNode, $name, $elements=array()) { $node = $this->Dom->createElement($name); for ($x=0; $x < count($elements); $x++) { if ($elements[$x][0] == ".") { $node->nodeValue = $elements[$x][1]; } else { $node->setAttribute($elements[$x][0], $elements[$x] [1]); } } $parentNode->appendChild($node); } general all purpose element creator for those of us that hate to call setAttribute a hundred times ;) That's pretty slick. I'll have to steal it : ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
On Monday 19 September 2005 06:04 pm, Ken Tozier wrote: Not a bad Idea. You might like this function I made then ;) function createElement($parentNode, $name, $elements=array()) { $node = $this->Dom->createElement($name); for ($x=0; $x < count($elements); $x++) { if ($elements[$x][0] == ".") { $node->nodeValue = $elements[$x][1]; } else { $node->setAttribute($elements[$x][0], $elements[$x][1]); } } $parentNode->appendChild($node); } general all purpose element creator for those of us that hate to call setAttribute a hundred times ;) > Even better, you can just extend the DOMDocument class, which is > perfect, since I'm basically just adding convenience methods. > > Thanks all for your help > > Ken > > > $x = new MyDom(); > > $x->createScriptElement('howdy.js'); > $x->createCSSLinkElement('howdy.css'); > $x->createStyledDivElement('bugColumnTitle', "I'm a styled piece > of text!"); > > print($x->saveXML()); > > class MyDom extends DOMDocument > { > function createScriptElement($inPath) > { > $new_elem = $this->createElementAndAppend('script', null); > $new_elem->setAttribute('type', 'text/javascript'); > $new_elem->setAttribute('src', $inPath); > } > > function createCSSLinkElement($inPath) > { > $new_elem = $this->createElementAndAppend('link', null); > $new_elem->setAttribute('href', $inPath); > $new_elem->setAttribute('rel', 'stylesheet'); > $new_elem->setAttribute('media', 'screen'); > } > > function createStyledDivElement($inStyle, $inData) > { > $new_elem = $this->createElementAndAppend('div', $inData); > $new_elem->setAttribute('class', $inStyle); > } > > > function createElementAndAppend($inType, $inData) > { > // setting null inData to an empty string forces a close > tag which is what we want > $elem_data= ($inData == null) ? '' : $inData ; > $new_elem= $this->createElement($inType, $elem_data); > $this->appendChild($new_elem); > return $new_elem; > } > } > ?> > > On Sep 19, 2005, at 6:13 PM, Ken Tozier wrote: > > Thanks Jasper > > > > Works perfectly on my Mac now as well. > > > > Ken > > > > On Sep 19, 2005, at 4:58 PM, Jasper Bryant-Greene wrote: > >> >> $x = new MyDom(); > >> $x->createScriptElement('howdy.js'); > >> print($x->saveXML()); > >> > >> class MyDom { > >> private $dom; > >> > >> /* __construct() = PHP5 constructor */ > >> function __construct() { > >> $this->dom = new DOMDocument('1.0', 'iso-8859-1'); > >> /* No need to return $this from a constructor */ > >> } > >> > >> function createScriptElement($scriptPath) { > >> $script = $this->dom->createElement('script', ''); > >> $script->setAttribute('type', 'text/javascript'); > >> $script->setAttribute('src', $scriptPath); > >> $this->dom->appendChild($script); > >> /* > >> Doesn't make sense for a createScriptElement() > >> method to also print out the XML, so I made a > >> separate method and called that from the > >> mainline code. > >> */ > >> } > >> > >> function saveXML() { > >> return $this->dom->saveXML(); > >> } > >> } > >> ?> > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
On Monday 19 September 2005 12:25 pm, Ken Tozier wrote: > > I would be extremely careful with this.. because sadly PHP's XML > > generator > > uses the short form whenever possible. > > will *NOT* work in most browsers such as FireFox. > > Will work. > > Thanks for the heads up. Looks like if you define the tag like > $script= $dom->createElement('script',''); <-- empty string > It tacks on an end tag. The way that I've been doing this thus far is using XSL. If you use the DOMDocument::loadXML() function and it has anything like that. it will convert it to short form. I design the back XML using Dom calls then throw a xsl at it to spit out my page. very nice clean code that's separated from style. > > So far, just playing. But ultimately I'd like to generate the whole > page using dom calls. It seems much cleaner for what I'm doing than > creating a whole bunch of large functions that do nothing more than > echo prestyled html. Is this clean enough? :) minimum for one of my pages that uses SQL: gDom() and $Document->gXPath() to gain access to them. $Document = new Document("rootNode"); try { $Document->gDb()->connect(Database::SQLITE,"../database.db"); } catch (DBConnectFailed $dbCF) { die("Failed to connect to the Database."); } $Document->setTitle("This is the title"); // Use $Document->setStylesheet(""); to set a stylesheet, // defaults to index.xsl. // Here is where you put the page specific code. // XML data nodes used in the XSL template. // This outputs the header(); and the DocType. $Document->outputHeader(); echo $Document; // This will load the XSL document, apply it, and return // The page to be echo'd ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
Even better, you can just extend the DOMDocument class, which is perfect, since I'm basically just adding convenience methods. Thanks all for your help Ken createScriptElement('howdy.js'); $x->createCSSLinkElement('howdy.css'); $x->createStyledDivElement('bugColumnTitle', "I'm a styled piece of text!"); print($x->saveXML()); class MyDom extends DOMDocument { function createScriptElement($inPath) { $new_elem = $this->createElementAndAppend('script', null); $new_elem->setAttribute('type', 'text/javascript'); $new_elem->setAttribute('src', $inPath); } function createCSSLinkElement($inPath) { $new_elem = $this->createElementAndAppend('link', null); $new_elem->setAttribute('href', $inPath); $new_elem->setAttribute('rel', 'stylesheet'); $new_elem->setAttribute('media', 'screen'); } function createStyledDivElement($inStyle, $inData) { $new_elem = $this->createElementAndAppend('div', $inData); $new_elem->setAttribute('class', $inStyle); } function createElementAndAppend($inType, $inData) { // setting null inData to an empty string forces a close tag which is what we want $elem_data= ($inData == null) ? '' : $inData ; $new_elem= $this->createElement($inType, $elem_data); $this->appendChild($new_elem); return $new_elem; } } ?> On Sep 19, 2005, at 6:13 PM, Ken Tozier wrote: Thanks Jasper Works perfectly on my Mac now as well. Ken On Sep 19, 2005, at 4:58 PM, Jasper Bryant-Greene wrote: createScriptElement('howdy.js'); print($x->saveXML()); class MyDom { private $dom; /* __construct() = PHP5 constructor */ function __construct() { $this->dom = new DOMDocument('1.0', 'iso-8859-1'); /* No need to return $this from a constructor */ } function createScriptElement($scriptPath) { $script = $this->dom->createElement('script', ''); $script->setAttribute('type', 'text/javascript'); $script->setAttribute('src', $scriptPath); $this->dom->appendChild($script); /* Doesn't make sense for a createScriptElement() method to also print out the XML, so I made a separate method and called that from the mainline code. */ } function saveXML() { return $this->dom->saveXML(); } } ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
Thanks Jasper Works perfectly on my Mac now as well. Ken On Sep 19, 2005, at 4:58 PM, Jasper Bryant-Greene wrote: createScriptElement('howdy.js'); print($x->saveXML()); class MyDom { private $dom; /* __construct() = PHP5 constructor */ function __construct() { $this->dom = new DOMDocument('1.0', 'iso-8859-1'); /* No need to return $this from a constructor */ } function createScriptElement($scriptPath) { $script = $this->dom->createElement('script', ''); $script->setAttribute('type', 'text/javascript'); $script->setAttribute('src', $scriptPath); $this->dom->appendChild($script); /* Doesn't make sense for a createScriptElement() method to also print out the XML, so I made a separate method and called that from the mainline code. */ } function saveXML() { return $this->dom->saveXML(); } } ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
Ken Tozier wrote: Rats. Looks like you can't encapsulate DOM objects in your own classes. I tried this and the browser 'view source' shows a completely empty document. createScriptElement('howdy.js'); class MyDom { function MyDom() { $dom = new DOMDocument('1.0', 'iso-8859-1'); return $this; } function createScriptElement($inScriptPath) { $script= $this->dom->createElement('script',''); $script->setAttribute('language', 'javascript'); $script->setAttribute('src', 'howdy.js'); $this->dom->appendChild($script); echo $this->dom->saveXML(); } } ?> Made a few changes to it as follows and it works perfectly here (PHP 5.0.5, Gentoo Linux): createScriptElement('howdy.js'); print($x->saveXML()); class MyDom { private $dom; /* __construct() = PHP5 constructor */ function __construct() { $this->dom = new DOMDocument('1.0', 'iso-8859-1'); /* No need to return $this from a constructor */ } function createScriptElement($scriptPath) { $script = $this->dom->createElement('script', ''); $script->setAttribute('type', 'text/javascript'); $script->setAttribute('src', $scriptPath); $this->dom->appendChild($script); /* Doesn't make sense for a createScriptElement() method to also print out the XML, so I made a separate method and called that from the mainline code. */ } function saveXML() { return $this->dom->saveXML(); } } ?> -- Jasper Bryant-Greene Freelance web developer http://jasper.bryant-greene.name/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
On Sep 19, 2005, at 3:26 PM, Vance Rodriguez wrote: I am used to seeing global class variables initialized in the class level if they are available to the class's $this statement. createScriptElement('howdy.js'); class MyDom { protected $dom; function __construct() { $this->dom = new DOMDocument('1.0', 'iso-8859-1'); } function createScriptElement($inScriptPath) { $script= $this->dom->createElement('script',''); $script->setAttribute('language', 'javascript'); $script->setAttribute('src', 'howdy.js '); $this->dom->appendChild($script); echo $this->dom->saveXML(); } } ?> On Sep 19, 2005, at 3:35 PM, comex wrote: $dom = new DOMDocument('1.0', 'iso-8859-1'); Shouldn't this be $this->dom = new DOMDocument('1.0', 'iso-8859-1'); ? That didn't work either. tried a few other things as well and it looks like PHP forces you to define DOM documents in one hideous monolithic code block without any of the encapsulation benefits classes provide. Not sure if this intentional due to security issues or if it's a bug. Anyone think of a reason why it would be necessary to prohibit DOM object embedding in a class? Ken -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
> $dom = new DOMDocument('1.0', 'iso-8859-1'); Shouldn't this be > $this->dom = new DOMDocument('1.0', 'iso-8859-1'); ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
Rats. Looks like you can't encapsulate DOM objects in your own classes. I tried this and the browser 'view source' shows a completely empty document. createScriptElement('howdy.js'); class MyDom { function MyDom() { $dom = new DOMDocument('1.0', 'iso-8859-1'); return $this; } function createScriptElement($inScriptPath) { $script= $this->dom->createElement('script',''); $script->setAttribute('language', 'javascript'); $script->setAttribute('src', 'howdy.js'); $this->dom->appendChild($script); echo $this->dom->saveXML(); } } ?> If the element adding code is in the same function as the dom create, it works createElement('script',''); $script->setAttribute('language', 'javascript'); $script->setAttribute('src', 'howdy.js'); $dom->appendChild($script); echo $dom->saveXML(); return $this; } } ?> Also if the DOM object is declared outside the class, and class functions access it as a global it works createScriptElement('howdy.js'); class MyDom { function MyDom() { return $this; } function createScriptElement($inScriptPath) { global $dom; $script= $dom->createElement('script',''); $script->setAttribute('language', 'javascript'); $script->setAttribute('src', $inScriptPath); $dom->appendChild($script); echo $dom->saveXML(); } } ?> Would be nice if you could encapsulate it though... You can probably add an empty text node to the script tag, or a text node consisting of a single space, or something like that, to make it use the long form. -- Jasper Bryant-Greene Freelance web developer http://jasper.bryant-greene.name/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
I would be extremely careful with this.. because sadly PHP's XML generator uses the short form whenever possible. will *NOT* work in most browsers such as FireFox. Will work. Thanks for the heads up. Looks like if you define the tag like $script= $dom->createElement('script',''); <-- empty string It tacks on an end tag. I personally would love to see a function where I could set it to use the long form. and when importing if it's in long form.. set that long from flag on automatically .. this would have saved me _hours_ of debugging work. are you trying to just generate the entire html page using only the XML DOM? .. or are you doing this in conjunction with another language such as XSL? So far, just playing. But ultimately I'd like to generate the whole page using dom calls. It seems much cleaner for what I'm doing than creating a whole bunch of large functions that do nothing more than echo prestyled html. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Using DOM object, how?
Stephen Leaf wrote: On Monday 19 September 2005 05:27 am, Jasper Bryant-Greene wrote: Ken Tozier wrote: I don't see any obvious DOM method for including scripts or css links like "". Do you have to put them in some other type of node like a processing instruction or a comment?
createElement('script'); $script->setAttribute('type', 'text/javascript'); $script->setAttribute('src', 'bobo.js'); $dom->appendChild($script); ?>
I would be extremely careful with this.. because sadly PHP's XML generator uses the short form whenever possible.
will *NOT* work in most browsers such as FireFox.