XmlRepository for PHP
Examples

This examples shall only demonstrate the usage of the class XmlRepository. I will not make you believe that the class is the ideal tool for implementing a XML based movie database application.

Initializing a new XmlRepository object

require_once('XmlRepository.class.php');
$repository = new XmlRepository();

After we have created the new object, we can define the table name of the repository (default is xmlrepository):

$repository->repository = "mymoviedatabase"; // optional

To retrieve error messages we set the debug option:

$repository->debug = true; // optional

Bulkloading complete XML content from repository

XML Data will be loaded with method getObject. You can use a subset of XPath commands to retrieve the data. The resulting SQL statements can be very expensive, because every single axe needs an additional self-join.

First we want to load the complete content of the repository, to see if there is any data:

$xml = $repository->getObject();
// or use
// $xml = $repository->getObject('/');

result:

As we can see, there isn't any data yet. Let us add a first movie. The Edge Approach ignores schemas and dtds. So it is up to you to add well-formed xml documents. But this may also be an advantage.

Sample data:

$xmlsample = "

<movies>
	<movie id="1">
		<title>The Big Sleep</title>
		<director>Howard Hawks</director>
		<cast>
			<actor id="1">Humphrey Bogart</actor>
			<actor id="2">Lauren Bacall</actor>
			<actor id="3">Dorothey Malone</actor>
		</cast>
	</movie>

</movies>

";

Adding sample to repository

$repository->addObject("/", $xmlsample);

Loading sample from repository

$repository->getObject("/movies");

result:

This example stores the XML sample into the root of the repository. We then read all nodes that descend from movies. Now we want to add another movie to the repository.

Sample data:

$xmlsample = "

<movie id='2'>
	<title>Dead Men Don't Wear Plaid</title>
	<director>Carl Reiner</director>
	<cast>
		<actor id='3'>Steve Martin</actor>
		<actor id='4'>Rachel Ward</actor>
	</cast>

</movie>

";

Adding sample to repository

$repository->addObject("/movies", $xmlsample);

Loading a XML subset from repository

$repository->getObject("/movies/movie/title");

result:

"Dead Men Don't Wear Plaid" is a really funny parody or hommage to the film noirs of the 1940s. If you know the movie, you will notice that 'Carl Reiner' is missing in the cast -- he played the buttler. We will add him now -- but we will make as if we would not know if he is already listed somewhere in the database.

First we must determine the last used id in our list of actors, To ensure that he won't be listed twice, we need to request for the node value:

$result = $repository->getObject("/movies/movie/cast/actor[. ='Carl Reiner']/text()");
if (empty($result)) {
	// get next id
	$id = $repository->getObject("/movies/movie/cast/actor/@id[last()]") + 1;
	$xmlsample = "
		<actor id=\"$id\">Carl Reiner</actor>

	";
	$repository->addObject("/movies/movie[title='Dead Men Don't Wear Plaid']/cast", $xmlsample);
}

Loading a XML subset from repository

$repository->getObject("/movies/movie/cast");

result:

Dorothey Malone and Steve Martin have identical ids. We need to change the attribute's value of one of them. We will do this for Steve Martin's id:

Updating objects

$repository->setData("/movies/movie/cast/actor[. = 'Steve Martin']/@id", '6');

Finally we load the complete content of our repository again:

Bulkloading complete XML content from repository

$xml = $repository->getObject();
// or use
// $xml = $repository->getObject('/');

result:

You can use the 'stream' option to send the data as XML content:

$repository->getObject("/","stream");

This will send the content to the client with utf-8 encoding:
header("Content-Type: application/xml; charset=utf-8");
see example.php