Sorting the data
It'd be nice to have the DVD's listed in alphabetical order--or in movie
length order. In my article "Working
with files and directories in Java" I showed how to sort a collection of
objects by letting them implement the "Comparable" interface. Since we now have
to sort according to two different criteria we'll need another technique.
Comparison of DVD-elements can be done in a separate class that implements the
Comparator interface and has a "compare" method:
package hansen.playground;
import java.util.*;
class CompareTitle implements Comparator {
public int compare(Object o1, Object o2) {
DVD d1 = (DVD)o1;
DVD d2 = (DVD)o2;
return (d1.getTitle()).compareToIgnoreCase(d2.getTitle());
}
}
The MySAXParser class can now sort the titles with a statement like this:
Collections.sort(dvdList, new CompareTitle());
To also sort according to movie length we define yet another class:
package hansen.playground;
import java.util.*;
class CompareLength implements Comparator {
public int compare(Object o1, Object o2) {
DVD d1 = (DVD)o1;
DVD d2 = (DVD)o2;
return d1.getLength() - d2.getLength();
}
}
To control which sorting class to use it's convenient to code a
sort-method:
public void sort(String type) {
if (type.equals("length"))
Collections.sort(dvdList, new CompareLength());
if (type.equals("title"))
Collections.sort(dvdList, new CompareTitle());
}
Presenting the results in a browser
It's now a simple matter to use the MySAXParser class in a servlet
application so we can show the list of DVD's in a browser. Let me first show
what it should look like:
By clicking on the "Title" or "Length" heading the list will be sorted. To
make it simple we'll make a jsp-page that uses the MySAXParser class. To display
the DVD's we'll have to code something like this:
sax.processFile("d:\\ex1.xml");
c = sax.getDVDs();
for (Iterator i = c.iterator(); i.hasNext();) {
DVD dvd = (DVD)i.next();
. . . get title, length and actors and show this in the HTML
}
The complete source can be found in the Resources section. The hyperlinks on
"Title" and "Length" calls a JavaScript function that calls another jsp-page (a
"controller") that'll call the sort routine. The links for "Title" and "Length"
are these:
<a href="javascript:sort('title')">Title</a>
<a href="javascript:sort('length')">Length</a>
The JavaScript function is simple:
<script>
function sort(type) {
location.href="ctlpage1.jsp?sort="+type;
}
</script>
The "ctlpage1.jsp" file contains the call to the sort-method:
<%
String type = request.getParameter("sort");
if (type != null && !type.equals("")) sax.sort(type);
pageContext.forward("page1.jsp");
%>
Conclusion
The SAX API will read an XML document and return the information bit by bit
to your program. This gives you full control of what you want to keep from the
document and what you want to skip. You may build a complete tree structure in
memory (like the DOM API does) or you may pick just the elements you're
interested in. Note however, that when the complexity of the XML-file grows,
coding also become more complex, and you should consider using the DOM API instead.
Resources
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|