Open In App

XQuery | Installation and Example

Improve
Improve
Like Article
Like
Save
Share
Report

XQuery: XQuery is the language for querying XML data. It is used to retrieve information stored in XML format. XQuery for XML is similar to 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 a database that is to be used in Web services like SoapUI.
  • Receiving data from a database that is to be used with application integration.
  • To generate summary reports.
  • To retrieve relevant information from XML files.

Installation and Example: 

  • Download the Saxon jar file.
  • Extract the zip file and copy all jar files to java_base_path/JRE/lib/ext.
  • Now create an XML file with the following code and name it as “article.xml”. 

html




<?xml version="1.0" encoding="UTF-8"?>
<articles>
  
    <article category="JAVA">
        <title>Introduction to Java</title>
        <writer>Khushi</writer>
        <date>05/11/2010</date>
    </article>
  
    <article category="Python">
        <title>Introduction to Python</title>
        <writer>Suman</writer>
        <date>10/10/2011</date>
    </article>
  
    <article category="XML">
        <title>Introduction to XML</title>
        <writer>KR</writer>
        <date>06/09/2012</date>
    </article>
  
    <article category="HTML">
        <title>Introduction to HTML</title>
        <writer>Bijay</writer>
        <date>03/04/2015</date>
    </article>
  
</articles>


Now create a java file with any name of your choice(example.java in my case) and add the following code in the same. 

Java




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));
        }
    }
}


Note: 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 the 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. 



Last Updated : 07 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads