Browse DevX
Sign up for e-mail newsletters from DevX


Integrate Cocoon with PostgreSQL for XML-driven Apps : Page 3

XML and relational databases are a natural fit. Learn how to access PostgreSQL data from Cocoon, so you can develop robust XML-driven applications with a relational backend.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Integrate PostgreSQL (cont'd)
Now you need to set up the "mytestdb" database. Make sure that PostgreSQL is running and that your user account has superuser privileges (or at least privileges to create a database). Create the mytestdb database with the following command:

createdb mytestdb

If you do not have sufficient privileges to create the database, have someone else create the database for you. You also have to create some sample tables and insert some sample data so that you can test the integration of Cocoon with PostgreSQL. To save on typing, the downloadable code provides create_table.sql and sample_data.sql. It also provides drop_table.sql, in the event you make a mistake or just want to start fresh again with the database tables. Issue the following commands to create the tables and populate them with data:

  1. psql -d mytestdb < /path/to/create_table.sql
  2. psql -d mytestdb < /path/to/sample_data.sql

At this point, you are ready to start up Tomcat again. Type the following to start Tomcat:

  1. cd $TOMCAT_HOME/bin
  2. ./startup.sh

You need to create the Cocoon code to access the database. One of the nice things about Tomcat is that it supports live reloading of code. Cocoon also checks for updated code. This enables you to add new XSPs and incrementally develop your application. Find the myapp.tgz file in the downloadable code. Perform the following steps:

  1. cd $COCOON_HOME
  2. tar xzf /path/to/cocoon_myapp.tgz

This article assumes you have familiarity with basic Cocoon concepts such as sitemaps, XSPs, and pipelines. If you have no idea what those concepts are, the Cocoon site is a good resource: http://cocoon.apache.org/2.1/. The tar command you just executed reads the archive file and creates a myapp directory in $COCOON_HOME. It then unpacks a few files: index.xsp, person_table.xslt, sitemap.xmap, and xml2html.xslt. The meat of the application is in index.xsp and person_table.xslt, which you should look over. It is a fairly simple application, but it does provide you with evidence that Cocoon is communicating with the PostgreSQL database named mytestdb.