<?php
/**
* In front of use of this example necessary to create a database in mySQL.
* SQL queries for creation of tables can be taken from file base.sql in root
* directory. After this necessary to fill the tables by data from file tree.sql
* in root directory. After it need specified the linking up parameters to
* database beneath in this example or in file config.ini.
*/
$user = 'root';
$pass = '';
$host = 'localhost';
$name = 'myXTree';
$prefix = 'xt_';
$root = $_SERVER['DOCUMENT_ROOT'];
$config['PATH']['pear'] = $root.'/PEAR';
$config['PATH']['myxml'] = $root.'/myXML';
$config['PATH']['myxtree'] = $root.'/myXTree';
function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = getmicrotime();
if (file_exists('config.ini')) {
$config = parse_ini_file('config.ini', true);
extract($config['DB'], EXTR_OVERWRITE);
}
$dsn = "mysql://$user:$pass@$host/$name";
if (substr(PHP_OS, 0, 3) == 'WIN') {
$searchPath = implode(';', $config['PATH']).';';
} else {
$searchPath = implode(':', $config['PATH']).':';
}
// Set the search path.
ini_set('include_path', $searchPath);
require_once('myDOM/myDOM.php');
require_once('myXTree.php');
require_once('DB.php');
require_once('Output.php');
PEAR::setErrorHandling(PEAR_ERROR_RETURN, E_USER_ERROR);
// Create new DOM document.
$oDocument = new Document;
$oDocument->setOption('indent', true);
$oDocument->setOption('method', 'xml');
// Connecting to database.
$db = DB::connect($dsn);
PEAR::isError($db) and
raiseError($db->getMessage());
// Set the selecting path.
$path = '/books//';
if ($_GET['xpath']) {
$path = stripslashes($_GET['xpath']);
}
// Create object myXTree.
$oXTree = myXTree::create(&$db, $prefix);
// Set method of selecting.
if ($_GET['method'] == 'recursive') {
$oXTree->recursive();
}
// Set the reception node.
$oXTree->setReceptionNode(&$oDocument);
// Selecting objects from SQL-tree.
$oXTree->select($path);
// Disconnecting.
$db->disconnect();
?>
<html>
<head>
<title>myXTree example</title>
</head>
<body style="margin: 10%; font-family: Courier New;">
<center>
<h1>myXTree example</h1>
</center>
<div>
<ul>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">/books/book</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">/books/book/*[name()!="price"]</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book/descendant-or-self::node()</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=1 or @id=3 or @id>7]//</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[price > 10]//</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[author="Plato" or author="Sophocles"]//</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=5]/following::node()</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=5]/preceding::node()</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=5]/following-sibling::node()</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//book[@id=5]/preceding-sibling::node()</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//title/parent::book[@id >= 5]</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//title/../author[.="Sophocles"]//</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//price/ancestor::book[@id!=5]</li>
<li style="cursor: hand;" onClick="document.pathForm.xpath.value=this.innerText;">//title[parent::book/@id=5]/ancestor::node()</li>
</ul>
<script language="php">
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "<br>Time: $time s.";
if ($oldtime = $_GET['time']) {
$rate = ($oldtime - $time)/$oldtime*100;
echo "<br>Rate: $rate %.";
}
</script>
<form name="pathForm">
<input type="hidden" name="time" value="<?php echo $time; ?>">
<input type="radio" name="method" value="selfjoin" checked> Selfjoin method
<input type="radio" name="method" value="recursive"> Recursive method <br>
<input type="text" name="xpath" style="width: 400;">
<input type="submit" name="submit" value="Submit">
</form>
</div>
<div style="color: blue; background-color: silver; padding: 1%;">
<script language="php">
print '<pre>'.htmlentities($oDocument->toString()).'</pre>';
</script>
</div>
</body>
</html>
|