Intercepting All Accesses To External Entities During XML SAX Parsing

 
 
This capability is useful in situations where the public or system id in an XML file do not refer to actual resources and must be redirected. An EntityResolver object must be installed on the parser in order to intercept the accesses. If a mapping is made,the entity resolver must return an InputSource object to the resource.
 
 
  1. try {

  2.         // Create an XML parser

  3.         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

  4.    

  5.         // Install the entity resolver

  6.         builder.setEntityResolver(new MyResolver());

  7.    

  8.         // Parse the XML file

  9.         Document doc = builder.parse(new File("infilename.xml"));

  10.     } catch (SAXException e) {

  11.         // A parsing error occurred; the xml input is not valid

  12.     } catch (ParserConfigurationException e) {

  13.     } catch (IOException e) {

  14.     }

  15.    

  16.     public class MyResolver implements EntityResolver {

  17.         // This method is called whenever an external entity is accessed

  18.         // for the first time.

  19.         public InputSource resolveEntity (String publicId,String systemId) {

  20.             try {

  21.                 // Wrap the systemId in a URI object to make it convenient

  22.                 // to extract the components of the systemId

  23.                 URI uri = new URI(systemId);

  24.    

  25.                 // Check if external source is a file

  26.                 if ("file".equals(uri.getScheme())) {

  27.                     String filename = uri.getSchemeSpecificPart();

  28.                     return new InputSource(new FileReader(filename));

  29.                 }

  30.             } catch (URISyntaxException e) {

  31.             } catch (IOException e) {

  32.             }

  33.    

  34.             // Returning null causes the caller to try accessing the systemid

  35.             return null;

  36.         }

  37.     }

  38.  

  39. This is the sample input for the example:

  40.     <?xml version="1.0" encoding="UTF-8"?>

  41.     <!DOCTYPE root SYSTEM "System.dtd" [

  42.         <!ENTITY entity1 SYSTEM "External.xml">

  43.         <!ENTITY entity2 SYSTEM "http://hostname.com/my.dtd">

  44.         <!ENTITY % entity3 SYSTEM "More.dtd">

  45.         %entity3;

  46.     ]>

  47.     <root>

  48.          
 
Copy to Clipboard      Download code as Text Format
 
  Date entered : 5th Apr 2007
  Rating : No Rating
  Accessed  :  4911
  Submitted by :  javabill
 
   Add Comment  Printer friendly
   View All Comments  Email to a friend
   Add to my Favourites Download PDF version
   Rating  
 
                 Click each image to add
this page to each site.
 
 
 
Comments Available

    No comment available at the moment.Be the first one to make a comment

 
Related Java Source Codes
  • Spring
  • Wraps a string to a given number of characters using a string break character
  • Uppercase the first character of each word in a string
  • Make a string's first character uppercase
  • Strip whitespace (or other characters) from the beginning and end of a string

  • Copyright © 2013 VisualBuilder. All rights reserved

    Warning: Unknown(): write failed: Disk quota exceeded (122) in Unknown on line 0

    Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0