advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Java and XML: putting SAX to work :

Contents
Why use XML?
Reading an XML file
Putting SAX to work
A complete event handler program for SAX
Sorting the data

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

 

How to Add Java Applets to Your Site

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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs