DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Processing XML From Java With the XQJ API

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.

Denis Sukhoroslov user avatar by
Denis Sukhoroslov
·
Mar. 01, 17 · Tutorial
Like (3)
Save
Tweet
Share
3.85K Views

Join the DZone community and get the full member experience.

Join For Free

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.

XML API Java (programming language) Processing

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Factors When Selecting a Database
  • Load Balancing Pattern
  • The Quest for REST
  • The Future of Cloud Engineering Evolves

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: