PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Richard Keizer  >  True DOM abstraction  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Usage example
Class: True DOM abstraction
Create XML documents using extensible DOM elements
Author: By
Last change: Made accessible without user login
Date: 11 years ago
Size: 3,684 bytes
 

Contents

Class file image Download
<?php
 
 
/**------------------------------------------------------------------------------
   * File: example.php
   * Description: usage example for dom-abstraction library
   * Version: 1.0
   * Author: Richard Keizer
   * Email: ra dot keizer at gmail dot com
   *------------------------------------------------------------------------------
   * COPYRIGHT (c) 2011 Richard Keizer
   *
   * The source code included in this package is free software; you can
   * redistribute it and/or modify it under the terms of the GNU General Public
   * License as published by the Free Software Foundation. This license can be
   * read at:
   *
   * http://www.opensource.org/licenses/gpl-license.php
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   *------------------------------------------------------------------------------
   *
   *
   * Finally i figured out a way to extend php's DOMElement in a proper way.
   * This example uses the W3C bookstore example.
   * It loads the xml and automatically binds true DOMElement extends to each element.
   * Finally we'll do a manual insert of a new book
   *
   */
 
 
error_reporting(E_ALL);
 
ini_set('display_errors', 1);

  include_once
'extendeddom.php';
 
 
  class
title extends extDOMElement {
    public function
setLanguage($lang) {
     
$this->setAttribute('lang', $lang);
      return
$this;
    }
  }
 
  class
cover extends extDOMElement {}
 
  class
year extends extDOMElement {}
 
  class
price extends extDOMElement {}
 
  class
author extends extDOMElement {}
 
  class
book extends extDOMElement {
   
    public function
setTitle($text, $lang) {
     
$this->createElement('title', null, $text)->setAttribute('lang', $lang);
      return
$this;
    }
   
    public function
setCover($covertype) {
     
$this->setAttribute('cover', $covertype);
      return
$this;
    }
   
    public function
setPrice($amount) {
     
$this->createElement('price', null, $amount);
      return
$this;
    }
   
    public function
setYear($year) {
     
$this->createElement('year', null, $year);
      return
$this;
    }
   
    public function
addAuthor($name) {
     
$this->createElement('author', null, $name);
      return
$this;
    }
   
    public function
setCategory($text) {
     
$this->setAttribute('category', $text);
      return
$this;
    }
  }
 
  class
bookstore extends extDOMElement {
   
    public function
addBook($category, $cover, $title, $lang, $author, $year, $price) {
     
$this->createElement('book')->setCategory($category)
                                  ->
setCover($cover)
                                  ->
setTitle($title, $lang)
                                  ->
addAuthor($author)
                                  ->
setYear($year)
                                  ->
setPrice($price);
    }
  }
 
 

 
 
 
$doc = new extDOMDocument();
 
$doc->loadXML(file_get_contents('http://www.w3schools.com/dom/books.xml'));
 
$xpath = new DOMxpath($doc);
 
$bookstore = $xpath->query('/bookstore')->item(0);
 
 
//var_dump(is_subclass_of($bookstore, 'DOMElement')); // uncomment this to prove that this is a true DOMElement extend!
 
 
$bookstore->addBook('SF (comedy)','paperback','The Hitchhiker\'s Guide to the Galaxy', 'en', 'Douglas Adams', '1979', '7.99');
  echo
$doc->saveXML();

 
 
 
 
 
 


For more information send a message to info at phpclasses dot org.