Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Processing XML From Java With the XQJ API

DZone's Guide to

Processing XML From Java With the XQJ API

This lesson in using the right tools for the job puts forward that if you know the kind of data you're going to be working with, start with a solution designed for it.

· Database Zone
Free Resource

Whether you work in SQL Server Management Studio or Visual Studio, Redgate tools integrate with your existing infrastructure, enabling you to align DevOps for your applications with DevOps for your SQL Server databases. Discover true Database DevOps, brought to you in partnership with Redgate.

This post is inspired by the article from Jay Sridhar: Load XML Into MySQL Using Java. I'd like to compare the way explained in the original article with the one provided by another Java API: XQJ (JSR-225). The steps explained in the source are not too complicated, but is it possible to get the same with less effort? Let's try to store XML documents in a native XML database. I'll walk through the same steps applied to the document/XML database Bagri DB.

1. Deploy Bagri DB

This step was not mentioned in the article, but I’ll add a line on this, just to be clear on how to start. Take the latest product release from GitHub, extract and run it.

2. Load Bagri XQJ Driver

XML databases provide their own kind of drivers to work with DBs from Java client apps. The driver API is specified in JSR-225: XQuery API for Java (XQJ). The API is quite similar to JDBC and designed for the same tasks.

So, just take the driver from the /distr folder of the extracted product and copy it to your application directory.

3. Connect to Bagri DB

This step is also similar to what it was for MySQL: set the connection properties and get a connection from DataSource:

XQDataSource xqds = new BagriXQDataSource();
xqds.setProperty(ADDRESS, “localhost:10500”);
xqds.setProperty(SCHEMA, “default”);
xqds.setProperty(USER, “guest”);
xqds.setProperty(PASSWORD, “password”);
xqds.setProperty(XQ_PROCESSOR, "com.bagri.xquery.saxon.XQProcessorClient");
xqds.setProperty(XDM_REPOSITORY, "com.bagri.client.hazelcast.impl.SchemaRepositoryImpl");
XQConnection xqConn = xqds.getConnection();


4. Create a (MySQL) Table

That is where the difference starts. There is no need to create any tables or other structures in Bagri, it’ll process XML and store it in internal DB structures automatically.

5. Parse XML

With XML DBs, there is no need to parse, it’ll do it for you. Just read the content from the file for future use. Bagri provides a set of handy utils for this:

String content = FileUtils.readTextFile(fileName);


6. Prepare XML Data

This step is also redundant in our case. We only need XML content to store it in the DB.

7. Insert Into Bagri DB

This step is closer to its counterpart from the source. Here, we’ll also prepare a statement and then execute it to store XML content to the DB:

String query = "declare namespace bgdb=\"http://bagridb.com/bdb\";\n" +
"declare variable $uri external;\n" + 
"declare variable $xml external;\n" + 
"let $uri := bgdb:store-document($uri, $xml)\n" +
"return $uri\n";

XQPreparedExpression xqpe = xqConn.prepareExpression(query);
xqpe.bindString(new QName("uri"), fileName, xqConn.createAtomicType(XQBASETYPE_ANYURI));
xqpe.bindString(new QName("xml"), content, xqConn.createAtomicType(XQBASETYPE_STRING));
xqpe.executeQuery();
xqpe.close();


8. Read XML Data From DB

This step is not covered in the source, but let's visit it just to have the post finished. In order to read data from an XML DB, we’ll prepare and execute an expression and then iterate over the returned result set, pretty much the same as we do it with JDBC:

String query = "declare variable $genre external;\n" +
      "let $catalog := fn:doc(\"" + fileName + "\") return\n" +
      "for $book in $catalog/book/genre = $genre\n” +
      "return $book/author/text()";

XQPreparedExpression xqpe = xqConn.prepareExpression(query);
xqpe.bindString(new QName("genre"), genre, xqConn.createAtomicType(XQBASETYPE_STRING));
XQSequence xqs = xqpe.executeQuery();
while (xqs.next()) {
      System.out.println("The author is: " + xqs.getAtomicValue());
}
xqs.close();
xqpe.close();


Summary

There are some cases when the use of native XML DB is not relevant: When the original data source is not XML, when the data structure is relational and static, etc. But, when the processed data has floating structure and comes in form of documents (XML, JSON, or any other self-describing data format), then it can be easier to use tools which are designed for that kind of tasks.

It’s easier than you think to extend DevOps practices to SQL Server with Redgate tools. Discover how to introduce true Database DevOps, brought to you in partnership with Redgate

Topics:
xml database ,document database ,nosql ,database ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}