XQuery | Installation and Example

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:

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

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?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_right

    
    

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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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_right

    
    

    Note: Save the file with .java extension in the same directory as that of article.xml.

  5. 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
    
  6. 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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.