XQuery: XQuery is the language for querying XML data. It is used to retrieve information stored in XML format. XQuery for XML is similar as SQL for databases. It can be used on XML Databases, relational databases containing data in XML formats or XML documents. XQuery was designed by W3C and it first appeared in 2007. It is a W3C recommendation from April 8, 2014 which is supported by all major databases.
Uses of Xquery:
- Extracting information from database that is to be used in Web services like SoapUI.
- Receiving data form database that is to be used with application integration.
- To generate summary reports.
- To retrieve relevant information from XML files.
Installation and Example:
- Download Saxon jar file.
- Extract the zip file and copy all jar files to java_base_path/JRE/lib/ext.
- Now create a XML file with the following code and name it as “article.xml”.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
articles
>
<
article
category
=
"JAVA"
>
<
title
>Introduuction to Java</
title
>
<
writer
>Khushi</
writer
>
<
date
>05/11/2010</
date
>
</
article
>
<
article
category
=
"Python"
>
<
title
>Introduuction to Python</
title
>
<
writer
>Suman</
writer
>
<
date
>10/10/2011</
date
>
</
article
>
<
article
category
=
"XML"
>
<
title
>Introduuction to XML</
title
>
<
writer
>KR</
writer
>
<
date
>06/09/2012</
date
>
</
article
>
<
article
category
=
"HTML"
>
<
title
>Introduuction to HTML</
title
>
<
writer
>Bijay</
writer
>
<
date
>03/04/2015</
date
>
</
article
>
</
articles
>
chevron_rightfilter_none -
Now create a java file with any name of your choice(example.java in my case) and add the following code in the same.
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.InputStream;
import
javax.xml.xquery.XQConnection;
import
javax.xml.xquery.XQDataSource;
import
javax.xml.xquery.XQException;
import
javax.xml.xquery.XQPreparedExpression;
import
javax.xml.xquery.XQResultSequence;
import
com.saxonica.xqj.SaxonXQDataSource;
class
example {
public
static
void
main(String[] args)
{
try
{
solve();
}
catch
(Exception e) {
e.printStackTrace();
}
}
private
static
void
solve()
throws
FileNotFoundException, XQException {
InputStream inputStream =
new
FileInputStream(
new
File(
"articles.xqy"
));
XQDataSource ds =
new
SaxonXQDataSource();
XQConnection conn = ds.getConnection();
XQPreparedExpression exp =
conn.prepareExpression(inputStream);
XQResultSequence result = exp.executeQuery();
while
(result.next()) {
System.out.println(result.getItemAsString(
null
));
}
}
}
chevron_rightfilter_noneNote: Save the file with .java extension in the same directory as that of article.xml.
- Create an XQuery file with the name “articles.xqy” with the following code and save it in the same directory as that of article.xml.
for $x in doc("article.xml")/articles/article return $x/title
- Now open Command prompt in the same folder where article.xml is located and enter the following command there.
javac example.java java example
As it could be seen in the output it will print the title of all the article’s data stored in the XML file.
Now, You have successfully executed your first XQuery code.