simplify DOM api

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

 



Hi Rob!

Rob wrote:
$node = new DOMElement("root");
In this case the element is not associated with a document. In DOM, you really aren't supposed to have a node not associated with any document, but this syntax allows the DOM classes to be extended in PHP. Once the node is associated with a document, you then have full editing capabilities.

Again, very helpful information - thanks a lot!

I have to do something like:

<?php

$array_of_book_objects = array('...');

$doc = new DOMDocument('1.0');
$books = $doc->appendChild(new DOMElement('books'));

foreach($array_of_book_objects as $book_data) {
  $book = $books->appendChild(new DOMElement('book'));
  $book->setAttribute('isbn', xml_entity_encode($book_data->isbn));
  $book->appendChild(new DOMElement('title',
    xml_entity_encode($book_data->title)));
  $book->appendChild(new DOMElement('description',
    xml_entity_encode($book_data->description)));
  $author = $book->appendChild(new DOMElement('author'));
  $author->appendChild(new DOMElement('name',
    xml_entity_encode($book_data->author_name)));
}
echo $doc->saveXML();
?>

Because my script is by far more complex (but does not use more advanced DOM features), I'd like to simplify the DOM api a little bit more. With extending DOMElement I think I have found a nice way:

<?php

class SimpleDOMElement extends DOMElement {

  function addChild($name, $value=NULL) {
    if (is_null($value)) {
      return $this->appendChild(new SimpleDOMElement($name));
    }
    else {
      return $this->appendChild(new SimpleDOMElement($name,
        xml_entity_encode($value)));
    }
  }

  function addAttribute($name, $value) {
    return $this->setAttribute($name, xml_entity_encode($value));
  }
}

class Books extends DOMDocument {

  private $books_element;

  function __construct() {
    parent::__construct('1.0');
    $this->books_element = $this->appendChild(
      new SimpleDOMElement('books'));
  }

  function addBook($book_object) {
    $book = $this->books_element->addChild('book');
    $book->addAttribute('isbn', $book_object->isbn);
    $book->addChild('title', $book_object->title);
    $book->addChild('description', $book_object->description);
    $author = $book->addChild('author');
    $author->addChild('name', $book_object->author_name);
  }
}

$array_of_book_objects = array('...');

$books = new Books;

foreach ($array_of_book_objects as $book_data) {
  $books->addBook($book_data);
}
echo $books->saveXML();
?>


I think this should be OK, or shouldn't I do it this way?


Perhaps you have seen that I've used a "xml_entity_encode()" function. This function works like htmlspecialchars(), but replaces ' with &apos; and not &#039;. Or do all of you use htmlspecialchars()? Does it work with Unicode strings?


best regards
Andreas

--
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