In this example, we will demonstrate some basic XML document processing.
As data, we provide five XML documents intended to be used with the very simple "person" schema. The document instances contain basic personal information such as last name, first name, and sex. You will find these XML documents along with a Tamino schema definition file in the examples/persons directory of the Tamino documentation.
In order to try out these examples, you will need a running database with the "person" schema defined. Please refer to the Tamino documentation for how to create a Tamino database.
The following use cases are covered in these examples:
establish a connection to a Tamino database
use a system accessor for retrieving different information
read some XML documents from files and insert them into the database
perform different queries on these documents and list the results
delete documents
insert a schema definition into the database, creating a new collection
insert some XML documents into the created collection
show how a document fails the validation against the schema
close a database connection
Two examples are provided that demonstrate these tasks
ProcessPersons
: Insert and query person documents
ProcessPersonsWithSchema
: Insert and query person
documents using a schema
The class ProcessPersons
performs the following tasks that
are typical for an application accessing data stored in a Tamino database:
establish a connection to the existing Tamino database
mydb
check the availability of the database connection
obtain a system access method and retrieve some system information
obtain an access method to handle XML data using the Document Object Model (DOM)
read some XML documents from files and insert them into the database
perform queries for all, some, the count of all and the availability of some documents
delete some documents by query and show the remaining documents
delete all documents
close a database connection
It is assumed that a database called mydb
has already been
created and is running. The necessary data files are stored within the
classpath at the same location as the class
ProcessPersons
and will be read from there. So the
examples are ready to run provided that Tamino is installed on the local
host.
You need to include the .jar files TaminoAPI4J.jar, JavaTaminoAPIExamples.jar, log4j.jar, xercesImpl.jarand xmlParserAPIs.jar in your class path, all of which are distributed with the Tamino API. Then you can run the Java interpreter:
java com.softwareag.tamino.db.api.examples.person.ProcessPersons ProcessPersons sample programm ============================== Connecting to Tamino database http://localhost/tamino/mydb, ... server is alive Here is some systeminformation ------------------------------ The Tamino server hosting http://localhost/tamino/mydb is version 4.2.1.1 (Server API version: 1.1, Tamino API for Java version: 4.2.1.1) Insert and query and delete in default collection "ino:etc" ----------------------------------------------------------- Reading documents from file and insert into database Inserted: Atkins, Paul (ino:id="1" collection="ino:etc" doctype="person" ) Inserted: Bloggs, Fred (ino:id="2" collection="ino:etc" doctype="person" ) Inserted: Müller, Andreas (ino:id="3" collection="ino:etc" doctype="person" ) Inserted: Müller, Karla (ino:id="4" collection="ino:etc" doctype="person" ) Inserted: Atkins, Andreas (ino:id="5" collection="ino:etc" doctype="person" ) The query "person" returns 5 documents, which are: Atkins, Paul (ino:id="1" collection="ino:etc" doctype="person" ) Bloggs, Fred (ino:id="2" collection="ino:etc" doctype="person" ) Müller, Andreas (ino:id="3" collection="ino:etc" doctype="person" ) Müller, Karla (ino:id="4" collection="ino:etc" doctype="person" ) Atkins, Andreas (ino:id="5" collection="ino:etc" doctype="person" ) The query "//surname='Atkins'" returns "TRUE" So list and then delete all "Atkins" documents The query "person[//surname='Atkins']" returns 2 documents, which are: Atkins, Paul (ino:id="1" collection="ino:etc" doctype="person" ) Atkins, Andreas (ino:id="5" collection="ino:etc" doctype="person" ) Deleted all documents for query "person[//surname='Atkins']" The query "person" returns 3 documents, which are: Bloggs, Fred (ino:id="2" collection="ino:etc" doctype="person" ) Müller, Andreas (ino:id="3" collection="ino:etc" doctype="person" ) Müller, Karla (ino:id="4" collection="ino:etc" doctype="person" ) Deleted all documents for query "person"
Note:
Using JavaTaminoAPIExamples.jar only works if
you have created a database called "mydb" on your
local host. Otherwise you have to change the constant DATABASE_URI
and recompile the code.
The class ProcessPersonsWithSchema
performs the following
tasks that are typical for an application accessing data stored in a Tamino
database:
establish a connection to the existing Tamino database
mydb
check the availability of the database connection
obtain a system access method and retrieve some system information
read a schema definition from file and insert it into the database, creating a new collection
read some XML documents from files and insert them into the database
show how a document fails the validation against the schema
query and list all successfully inserted documents
delete all documents
close a database connection
It is assumed that a database called mydb
has already been
created and is running. The necessary schema and data files are stored within
the classpath at the same location as the class
ProcessPersonsWithSchema
and will be read from there. So the
examples are ready to run provided that Tamino is installed on the local host.
Note that the class ProcessPersonsWithSchema
uses some methods
from the class ProcessPersons
and thus cannot run without it.
You need to include the .jar files TaminoAPI4J.jar, JavaTaminoAPIExamples.jar, log4j.jar, xercesImpl.jar, and xmlParserAPIs.jar in your classpath, all of which are distributed with the Tamino API. Then you can run the Java interpreter:
java com.softwareag.tamino.db.api.examples.person.ProcessPersonsWithSchema ProcessPersonsWithSchema sample programm ======================================== Connecting to Tamino database http://localhost/tamino/mydb, ... server is alive Here is some systeminformation ------------------------------ The Tamino server hosting http://localhost/tamino/mydb is version 4.2.1.1 (Server API version: 1.1, Tamino API for Java version: 4.2.1.1) Create collection "people" insert and delete some documents ----------------------------------------------------------- Reading TSD3 schema from file and insert into database Inserted the schema for collection "people" and doctype "person" Reading documents from files and insert into database Inserted: Atkins, Paul (ino:id="1" collection="people" doctype="person" ) Inserted: Bloggs, Fred (ino:id="2" collection="people" doctype="person" ) Inserted: Müller, Andreas (ino:id="3" collection="people" doctype="person" ) Can't insert: Müller, Karla (ino:id="" collection="people" doctype="person" ) Reason: Line 8, Column 15: [element 'occupation' in element 'person'] Inserted: Atkins, Andreas (ino:id="4" collection="people" doctype="person" ) The query "person" returns 4 documents, which are: Atkins, Paul (ino:id="1" collection="people" doctype="person" ) Bloggs, Fred (ino:id="2" collection="people" doctype="person" ) Müller, Andreas (ino:id="3" collection="people" doctype="person" ) Atkins, Andreas (ino:id="4" collection="people" doctype="person" ) Deleted all documents for query "person" Deleted collection "people" with all content
Note:
Using JavaTaminoAPIExamples.jar only works if
you have created a database called "mydb" on your
local host. Otherwise you have to change the constant DATABASE_URI
and recompile the code.