#!/usr/local/bin/perl5.8.0
# Very simple demonstration code to show some of the features of
# Berkeley DB XML. The file "demo.xml" referred to below might have
# this form:
#
#
#
#
use strict;
use warnings;
use Sleepycat::DbXml 'simple';
#Location of database
my $database='/home/ford/local/etc/xmldb/db';
#XML source, slurped in as string.
my $xml_content=&get_xml('/home/ford/sites/ftrain.com/cgi-bin/demo.xml');
&create_database($database, $xml_content);
&query_database($database);
sub get_xml {
# Pull in an XML document from the filesystem with no parsing.
my ($xml_source) = (@_);
local $/ = undef;
open(IN,$xml_source);
my $xml_content=;
close(IN);
return $xml_content;
}
sub create_database {
# Create an XML database and populate it with data.
my ($database, $xml_content) = (@_);
my $container = new XmlContainer($database);
$container->open(Db::DB_CREATE);
my $document = new XmlDocument;
$document->setContent($xml_content);
my $author = 'me@address.com';
my $val = new XmlValue($author);
$document->setMetaData("http://mynamespace.org", "ns", "author", $val);
$container->putDocument($document);
$container->close();
}
sub query_database {
#Query an XML database with an XPath statement.
my ($database) = (@_);
my $container = new XmlContainer($database);
$container->open(Db::DB_CREATE);
my $context = new XmlQueryContext(XmlQueryContext::ResultValues);
# Replace this Xpath with something relevant to your own needs.
my $results = $container->queryWithXPath("//section/id", $context);
my $value = new XmlValue;
$results->next($value);
print "Result is " . $value->asString() . "\n" ;
}