Пример #1
0
<?php

require_once "classes/autoload.php";
use com\soloproyectos\common\dom\node\DomNode;
header("Content-type: text/plain; charset=UTF-8");
$root = DomNode::createFromString(file_get_contents("test.xml"));
// Example 1
echo "*** Example 1: Traversing an XML document ***\n\n";
$books = $root->query("books item");
foreach ($books as $book) {
    echo "Title: " . $book->attr("title") . "\n";
    // gets genres
    echo "Genres: ";
    $genresIds = explode(" ", $book->attr("class"));
    foreach ($genresIds as $id) {
        $item = $root->query("genres item[id = '{$id}']");
        echo $item->text() . " ";
    }
    echo "\n";
    // prints author
    $item = $root->query("authors item[id = '" . $book->attr("author_id") . "'] name");
    echo "Author: " . $item->text() . "\n";
    // prints aditional info
    echo "ISBN: " . $book->query("isbn")->text() . "\n";
    echo "Available: " . $book->query("available")->text() . "\n";
    echo "Description: " . trim($book->query("description")->text()) . "\n";
    echo "---\n";
}
// Example 2
echo "\n*** Example 2: printing HTML contents ***\n\n";
$authors = $root->query("authors item[id = arthur-cclarke]");
Пример #2
0
 /**
  * Returns the value at specified offset.
  *
  * This function implements ArrayAccess::offsetGet.
  *
  * @param integer $offset Offset
  *
  * @return DomNode
  */
 public function offsetGet($offset)
 {
     return DomNode::createFromElement($this->elements[$offset]);
 }
Пример #3
0
 /**
  * Gets the root node.
  *
  * @return DomNode
  */
 public function root()
 {
     return DomNode::createFromElement($this->document->documentElement);
 }