Re: Re: Using DOM object, how?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
>
>
> <?php
>      $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:
> >> <?php
> >> $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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux