Learning to use XML engine Xqengine_xml/rss

Source: Internet
Author: User
Tags documentation xpath xslt apache tomcat
Lately I've been looking for XML search tools, and the apps I write need to search for some associated XML files on a regular basis, I mean to see if there's data in the file that matches the data I want, but sometimes I want to export the data that I've found. At first, I tried XSLT and XPath to turn the search problem into a problem that I could solve using XSLT, but after a while, I found that using XSLT didn't really solve the search problem I wanted to work with, because the data I wanted to output was a comma-separated number. XSLT does not meet this requirement, and XLST does not provide full-text search capabilities. Then I would like to try to use XML Query Language (XQL), to see if it can be resolved, so I carefully took a look at the implementation of the various versions of XQL, coincidentally, just found a small tool called xqengine can solve this problem, so, In this article I want to explain how to use Xqengine to search your XML file for the string data you want to find.

Xqengine can be found under the Www.fatdog.com Web site, which is a javabean that uses a SAX parser to index one or more XML documents, and then you can make a composite search of those documents. The search language it uses is a superset of XQL and has a similar syntax to XPath.

Java classes that use Xqengine must implement a result () method, and after the search is completed, the engine will call this method to pass the search results to the result () method, which can be used to output data in three formats for displaying the data. Use command-line arguments to indicate the search parameters you need, for example, you can indicate that a file is not indexed if it contains the word stop, or you can command the engine to ignore words that are less than the specified number in the argument.

Below, I give a routine that uses xqengine, now let's analyze it. First, the main () method instantiates a search engine: xmlengine engine = new Xmlengine (), then it obtains the file name from the command line, returns the result format and the search request these three parameters, and then uses various configuration methods to set up the engine. Then call the Setsaxparsername () method to set the full name of the SAX parser, because we are using the Xerces parser, so we need to use "Org.apache.xerces.parsers.SAXParser". Then we need to set the search parameters, and in this case we will not index the number or any word less than 3 characters. In your download to the Xqengine API documentation will have detailed configuration parameter description, so I will not elaborate on how to configure the parameters, please see the relevant documentation. Finally, the Setdocument () method specifies the XML file that Xqengine will index or search. Of course, if you want to index multiple files, just set up a few corresponding setdocument () methods.

As we can see from the following code, the Xqengine engine returns search results in three different formats: STANDARD, summary, and CSV (values separated by commas) for simplicity, I've defined a number for each return result type instead (1,2,3), The Setlistenertype () method is then invoked using the appropriate parameters. I'll explain each of the return result types in detail later. There is also a method printsessionstate () to output the index and engine information, but I did not write it into the routine, so the above program will only output search results; Next call the Addxqlresultlistener () method, and passing an instance of search to implement the Xqlresultlistener interface, and then the query string as a parameter to invoke the Setquery method, the engine will begin to execute the query task. When the query is finished, the engine calls the search class result () method, returns the query results, and in the routines I provide, the result () method simply outputs the results.
Code:

Import java.io.*;
Import Com.fatdog.textEngine.XmlEngine;
Import com.fatdog.textengine.exceptions.*;
Import Com.fatdog.textEngine.query.XQLResultListener;

Public class Search implements Xqlresultlistener
{
public static void Main (string[] args)
{
Xmlengine engine = new Xmlengine ();
String searchfile = args[0];
String searchtype = args[1];
String query = args[2];
try {file://configuration engine
Engine.setsaxparsername ("Org.apache.xerces.parsers.SAXParser");
Engine.setminindexablewordlength (3);
Engine.setdoindexnumbers (FALSE);
Engine.setdocument (searchfile);


if (Searchtype.equals ("1")) {
Engine.setlistenertype (
Xmlengine.standard_listener);
}
else if (Searchtype.equals ("2")) {
Engine.setlistenertype (
Xmlengine.summary_listener);
}
else {
Engine.setlistenertype (
Xmlengine.csv_listener);
}
}
catch (Missingorinvalidsaxparserexception e) {
System.out.println (
"Missing or unavailable sax parser");
Return
}
catch (FileNotFoundException e) {
System.out.println (
"Cannot find XML file:");
Return
}
catch (Cantparsedocumentexception e) {
System.out.println (
"Cannot parse XML file:");
Return
}
Engine.printsessionstats ();
Engine.addxqlresultlistener (New Search ());
try {
Engine.setquery (query);
}
catch (Invalidqueryexception e) {
System.out.println (
"Unavailable Query Request:" + e.getmessage ());
Return
}
}
public void results (String xqlresults)
{
System.out.println (Xqlresults);
}
}


 
Well, we've written a program that uses xqengine, so let's run this code, and before we compile this code, we need to download to the Xqengine and the SAX parser. I downloaded it from the xml.apache.org to the Xerces parser. I use the operating system is Windows PROFESSIONAL,JDK for version 1.3, OK, after these I will set up the Classpath bar, in the "environment variable" to modify the Classpath, add "C:\xql\XQEngine.jar;" C:\xql\antlr.jar; C:\xerces\xerces.jar ". Now we can compile the code, but in order to be able to run the program, we also need an XML file, I use the Apache Tomcat web.xml file as a demo. I also introduced the previous, we use 1,2,3 to replace three kinds of return query result format respectively:

1, the use of Standard_listener (number 1) and query item "//welcome-file-list/welcome-file", C:\xql\xql1>java search Web.xml 1 "/ Welcome-file-list/welcome-file "

Parser.installsaxparser:


Installed successfully
1:indexing Web.xml
Query: (////Welcome-file-list Welcome-file)
3 hit (s) for File://welcome-file-list/welcome-file
>
query= "//welcome-file-list/welcome-file"
Hitcount= "3"
Elemcount= "3"
Doccount= "1"
Xmlns:xql= "http://www.fatdog.com/Standard_Listener.html" >

index.jsp


Index.html


Index.htm



In the example above, the query item requires that all the "welcome-file" child elements of any "welcome-file-list" element be found. Note that the results of the search are basically extracted from the original XML document and cannot establish the relationship between the search results and the original document. The Summary_listener (2) return type is somewhat different, it includes a "DocID" and a "ELEMLX" number, so that the result can be linked to the original document.

The following is an example of the return result:


C:\xql\xql1>java Search Web.xml 2
"//welcome-file-list/welcome-file"
Parser.installsaxparser:
Installed successfully

1:indexing Web.xml

Query: (////Welcome-file-list Welcome-file)

3 hit (s) for File://welcome-file-list/welcome-file

>
query= "//welcome-file-list/welcome-file"
Hitcount= "3"
Elemcount= "3"
Doccount= "1"
Xmlns:xql= "http://www.fatdog.com/
Summary_listener.html ">





As I said earlier, the most important thing for my application is to return results that are separated by commas, so Csv_listener (3) is useful, and it can return a result that is separated by commas using the following:

C:\xql\xql1>java Search Web.xml 3
"//welcome-file-list/welcome-file"
Parser.installsaxparser:

Installed successfully

1:indexing Web.xml

Query: (////Welcome-file-list Welcome-file)

3 hit (s) for File://welcome-file-list/welcome-file

3,3,1,0
0,270,welcome-file
0,271,welcome-file
0,272,welcome-file

Of course, Xqengine also has a lot of powerful features, I can not be introduced here, it comes with a rich source of documentation and use methods, you can learn to use, of course, if you want to, you can even develop a GUI program, A GUI-based search program is available in the document: Swingquerydemo, you can look at the research.

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.