Java
  Home arrow Java arrow Page 4 - Advanced SAX
IBM developerWorks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Advanced SAX
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-07-11

    Table of Contents:
  • Advanced SAX
  • Setting Properties and Features
  • Error Handling
  • Resolving Entities

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Advanced SAX - Resolving Entities


    (Page 4 of 4 )

    You’ve already seen how to interact with content in the XML document you’re parsing (using ContentHandler ), and how to deal with error conditions ( ErrorHandler ). Both of these are concerned specifically with the data in an XML document. What I haven’t talked about is the process by which the parser goes outside of the document and gets data. For example, consider a simple entity reference in an XML document:

      <FM>
      <P>Text placed in the public domain by Moby Lexical Tools, 1992.</P>
      <P>SGML markup by Jon Bosak, 1992-1994.</P>
      <P>XML version by Jon Bosak, 1996-1998.</P>
     
    <P>&usage-terms;</P>
      </FM>

    Your schema then indicates to the parser how to resolve that entity:

      <!ENTITY usage-terms
          SYSTEM "http://www.newInstance.com/entities/usage-terms.xml">

    At parse time, the usage-terms entity reference will be expanded (in this case, to “This work may be freely copied and distributed worldwide.”, as seen in Figure 4-1).


    Figure 4-1.  The usage-terms entity was resolved to a URI, which was then parsed and inserted into the document

    However, there are several cases where you might not want this “default” behavior:

    1. You don’t have network access, so you want the entity to resolve to a local copy of the referenced document (perhaps a version you’ve downloaded yourself).
    2. You want to substitute your own content for the content specified in the schema.

    You can short-circuit normal entity resolution using org.xml.sax.EntityResolver . This interface does exactly what it says: resolves entities. More important, it allows you to get involved in the entity resolution process. The interface defines only a sin gle method, as shown in Figure 4-2.

    To insert your own logic into the resolution process, create an implementation of this interface, and register it with your XMLReader instance through setEntityResolver() . Once that’s done, every time the reader comes across an entity


    Figure 4-2.  There's not much to the EntityResolver class; just a single, albeit useful, method

    reference, it passes the public ID and system ID for that entity to the resolveEntity() method of your EntityResolver implementation.

    Typically, the XML reader resolves the entity through the specified public or system ID. If you want to accept this default behavior in your own EntityResolver implementation, just return null from your version of resolveEntity() . In fact, you should always make sure that whatever code you add to your resolveEntity() implementation, it returns null in the default case. In other words, start with an implementation class that looks like Example 4-1.

    Example 4-1. Before coding in special entity resolution, always ensure that any unhandled cases result in a null return value (and therefore normal entity resolution)

    package javaxml3;

    import java.io.IOException;

    import org.xml.sax.EntityResolver;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;

    public class SimpleEntityResolver implements EntityResolver {

      public InputSource resolveEntity(String publicID, String systemID )
        throws IOException, SAXException {

        // In the default case, return null
        return null;
      }
    }

    Please check back tomorrow for the continuation of this article.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This article is an excerpt from the book "Java and XML, Third Edition," published by...
     

    Buy this book now. This article is excerpted from chapter four of the book Java and XML, Third Edition, written by Brett McLaughlin and Justin Edelson (O'Reilly, 2006; ISBN: 059610149X). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway