Use XQuery in Java

Source: Internet
Author: User
Tags xquery

When using relational databases, we use SQL statements to retrieve data sources. There is no problem, but relational data also has some limitations and can only store structured data.
How to store unstructured data sets? The simplest way is to encapsulate the data into XML.
In application development, we often use XML as a data source to store some unstructured data. However, is there a language that can retrieve XML like SQL statements to retrieve relational databases? The answer is XQuery.

The syntax structure of the XQuery itself is not complex. xml node locations are described through XPath and supplemented with corresponding logical expressions to meet users' search preference settings.
For more information about XQuery and XPath, see the following URL:
Http://www.w3school.com.cn/xpath/index.asp
Http://www.w3school.com.cn/xquery/index.asp
The basic syntax information is as follows:
In XPath, '/' is used to represent the node hierarchy. '//' is used to represent all nodes.
The node filter conditions are written in '[]'.
Add @ to node attribute
For example, // person [@ name = 'hangsan']/password indicates obtaining the User Password named zhangsan
The corresponding XML structure is as follows: <root> <person name = "zhangsan"> <password> Mima </password> </person> person... </root>

In XQuery
The where statement is used to specify filtering conditions.
The return statement is used to set the returned result set.
The for statement is used to execute traversal.
The IF/else condition judgment logic can be added to both the where and return clauses.
Several common functions:
Data (element): return the text of the node.
Contains (element, value): Query fuzzy match
DOC (filepath): loads XML data files
For example:
For $ X in DOC ("persons. xml") // person
Where $ X/@ age> 20 and contains ($ X/@ name, 'zhang ')
Return if ($ X/@ sex = 'male') Then data ($ X/password) else ()
Indicates:
Query the persons with the surname Zhang and whose age is greater than 20. If the password is returned by a male, if the password is returned by a female, null is returned.

Java API usage
Here we mainly use Saxon to execute XQuery. For specific applications, refer to the following URL:
Http://www.cs.duke.edu/courses/fall08/cps116/docs/saxon/samples/java/XQJExamples.java
The Saxon jar package can be downloaded here:
Http://download.csdn.net/detail/javaman_chen/5107221
The usage is roughly as follows:

// First obtain the XML data source to connect to xqdatasource DS = new saxonxqdatasource (); xqconnection conn = Ds. getconnection (); // execute xqueryxqexpression expression = conn through xqexpression. createexpression (); xqresultsequence res=expression.exe cutequery ("Doc (persons. XML) // person .... "); // process the result set while (res. next () {res. getObject ();}

In addition:
The doc () function of XQuery needs to pass the XML document path. However, during development, our XML data source may not have generated a file. For this reason, xqexpression provides APIs, you can directly bind XML data without loading XML documents.
Expression. bindnode (xqconstants. context_item, org. W3C. Dom. Document doc, null );
Doc objects can be generated through the documentbuilder class.
Documentbuilderfactory DBF = documentbuilderfactory. newinstance ();
Documentbuilder DB = DBF. newdocumentbuilder ();
Document Doc = dB. parse (New bytearrayinputstream ("<person> zhangsan </person>". getbytes ()));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.