PHP Classes
elePHPant
Icontem

File: myXML/sample.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Tereshchenko Andrey  >  myXML  >  myXML/sample.php  >  Download  
File: myXML/sample.php
Role: Example script
Content type: text/plain
Description: Example script
Class: myXML
DOM/XPath/XSLT implemented in php.
Author: By
Last change: -
Date: 10 years ago
Size: 2,360 bytes
 

Contents

Class file image Download
<?php

function getmicrotime() {
   list(
$usec, $sec) = explode(" ", microtime());
   return ((float)
$usec + (float)$sec);
}
$time_start = getmicrotime();

$root = $_SERVER['DOCUMENT_ROOT'];
$path[] = $root.'/PEAR';
$path[] = $root.'/myXML';

if (
substr(PHP_OS, 0, 3) == 'WIN') {
   
$searchPath = implode(';', $path).';';
} else {
   
$searchPath = implode(':', $path).':';
}

// Set the search path.
ini_set('include_path', $searchPath);

require_once(
'XML_Preprocessor.php');
require_once(
'myDOM/myDOM.php');
require_once(
'myXPath/myXPath.php');
require_once(
'myXSLT/myXSLT.php');

PEAR::setErrorHandling(PEAR_ERROR_RETURN, E_USER_ERROR);

// Create new DOM documents for input, output and stylesheet data.
$oDocument = new Document;
$oStylesheet = new Document;
$oOutDocument = new Document;

// Create object of class XML_Preprocessor.
$oXml = XML_Preprocessor::create(&$oDocument);

// As in a file sample.xml is used PHP code, for its parsing the object of class
// XML_Preprocessor will be used.
$oXml->parseFile('sample.xml');

// Use of a new features.
$xmlData = "
  <book id='10'>
    <title><b>New features in myDOM</b> (see manual)</title>
    <author><i>Tereshchenko Andrey</i> ;~)</author>
    <price>0.00</price>
  </book>"
;
$oDocument->documentElement->parse($xmlData);

// For parse of sample.xsl file we use a method of the DOM document parseFile().
$oStylesheet->parseFile('sample.xsl');

// Create object myXPath.
$oXPath =& myXPath::create(&$oDocument);

// Select any "book" nodes.
$nodeSet = $oXPath->evaluate('//book');

$length = sizeof($nodeSet);
for (
$i = 0; $i < $length; $i++) {
   
$node =& $nodeSet[$i];
   
// Add child element "date"
   
$date =& $oDocument->createElement('date');
   
$text =& $oDocument->createTextNode(date("d M Y", time()));
   
$date->appendChild(&$text);
   
$node->appendChild(&$date);
}

// Create object myXSLT.
$oXSLT = myXSLT::create(&$oDocument, &$oOutDocument, &$oStylesheet, &$oXPath);

// Start translating.
$oXSLT->translate();

// For display of the document now it is possible to use a method toString()
// instead of object Output.
$oOutDocument->setOption('indent', true);
print
$oOutDocument->toString();

$time_end = getmicrotime();
$time = $time_end - $time_start;
print(
"<br>Time: $time s.");

?>