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:
- 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”.
Now create a java file with any name of your choice(example.java in my case) and add the following code in the same.
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 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.