Use xquery in java

Source: Internet
Author: User

When using relational databases, we use SQL statements to retrieve data sources. This is no problem, but relational data also has some limitations, only structured data can be stored. How can a dataset be stored when it is unstructured? The simplest way is to encapsulate it 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 at www.2cto.com, refer to the following website region] '. Add the @ symbol to the node attribute, for example, // person [@ name = 'hangsan']/password. the xml structure corresponding to the user password is as follows: <root> <person name = "zhangsan"> <password> mima </password> </person> person... </root> The where statement in xquery is used to specify the return Statement of the filter condition. It is used to set the returned result set for statement to execute traversal. Comparison of the if/else condition judgment logic can be added in both the where and return clauses. common functions: data (element): return the text contains (element, value) of the node: Query fuzzy match doc (filePath): load xml data files such as: for $ x in doc ("persons. xml ") // personwhere $ x/@ age> 20 and contains ($ x/@ name, 'zhang') return if ($ x/@ sex = 'male ') then data ($ x/password) else () indicates to query the password returned by a male who has a surname Zhang and is older than 20, if a blank javaAPI is returned by a female, saxon is mainly used to execute xquery. The specific application can be found at the following URL: lateral view plaincopy // first obtain the xml data source to connect to XQDataSource ds = new SaxonXQDataSource (); XQConnection conn = ds. getConnection (); // execute xquery XQExpression 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, the xml data source may not have generated any files, XQExpression provides APIs to bind xml data without loading xml document expressions. bindNode (XQConstants. CONTEXT_ITEM, org. w3c. dom. document doc, null); the doc object can generate DocumentBuilderFactory dbf = DocumentBuilderFactory through the DocumentBuilder class. 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.