Straight from the Heart

XML Entity Resolver

Posted on: July 15, 2008


 

Today, while creating a xml entity resolver, i have got struck in a trifling situation. I was using the throws keyword for handling exception instead of try catch block at InputStream instance creation method for a particular url path-getInputStreamFromDB(String urlString), and due to that, in case of error, control was going to the ValidatingErrorHandler class (a error handler used by the parser), which was again pushing it to the ExternalEntityresolver class (an Entity Resolver) and this whole thing was going into a recursion.

 

code snippets to give an idea-

ExternalEntityResolver externalEntitityResolver = new ExternalEntityResolver();

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            factory.setAttribute(http://java.sun.com/xml/jaxp/properties/schemaLanguage”, http://www.w3.org/2001/XMLSchema”);

 

            factory.setNamespaceAware(true);

            factory.setValidating(true);

           

            ValidatingErrorHandler validator = new ValidatingErrorHandler();

           

            DocumentBuilder documentBuilder = factory.newDocumentBuilder();

            documentBuilder.setErrorHandler(validator);

            documentBuilder.setEntityResolver(externalEntitityResolver);

 

 

 

privateInputStream getInputStreamFromDB(String urlString) throwsMalformedURLException, IOException {

            InputStream inputStream = null;

            String xmlPath=   “D:\\eclipse workspace\\PIF_VALIDATION\\resource\\AddressBook-ZipToCityEPD.xsd”;

            //xmlPath=new File(xmlPath).toURI().toString();

            URL url = newURL(xmlPath);          Origin of exception

            URLConnection con = url.openConnection();

           

            if(con != null){

                  inputStream = con.getInputStream();

            }

            returninputStream;

      }

 

Later, when I have used try-catch block to check the stacktrace, encountered the real culprit- a fresh exception. This exception originates at the time of URL instance creation for a given path String.

 

try{

path= “D:\\eclipseworkspace

String \\PIF_VALIDATION\\resource\\AddressBook-ZipToCityEPD.xsd”;

URL url = new URL(path);           

            URLConnection con = url.openConnection();

           

            if(con != null){

                  inputStream = con.getInputStream();

            }

            }catch (MalformedURLException e) {

                  e.printStackTrace();// TODO: handle exception

 

java.net.MalformedURLException: unknown protocol: d

      at java.net.URL.<init>(Unknown Source)

      at java.net.URL.<init>(Unknown Source)

      at java.net.URL.<init>(Unknown Source)

      at com.XXX.xml.ExternalEntityResolver.getInputStreamFromDB(ExternalEntityResolver.java:80)

 

It seems as if String path is not interpreted as a file reference and because of this, it is throwing MalformedURLException with this “unkown protocol d” message. Preferably I feel it should be handled by URL class code by checking the format of the path String. For solving this problem, we need to create a valid URL in code itself.

 

Like-

Just put this line of code before the creation of URL instance-

 

path=new File(path).toURI().toString();

URL url = new URL(path);

 

There is one more way, more simpler one to achieve the purpose

Simply append “file:///” at the beginning of path String and it would solve the purpose.

 

Path=”file:/// “+path;

URL url = new URL(path);

 

Tags: ,

2 Responses to "XML Entity Resolver"

Thanks for this tip, I was really blocked; and the error message was not so clear.

Hi Zef,

Thanks for your kind comment.

Manu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Translator

a

The Calendar..तारीख

July 2008
M T W T F S S
« Jun   Sep »
 123456
78910111213
14151617181920
21222324252627
28293031  

गुफ्तुगू ….

Sindhu on How to integrate Sonar with…
Naga Anji on EMMA: a free Java code coverag…
Manu on Hiring
jarvarm on Hiring
Manu on Hiring

पन्ना

हिसाब-किताब

  • 37,617 hits

Tashveerein.......तसवीरें

Ceannabeinne

Cuckoo, Cuckoo

All the Small Mistakes

The Old Teahouse in Pengzhen Town, Chengdu

Kestrel      (Falco tinnunculus)

American Crow

Sony A7 MII + Zeiss Sonnar 50/1.5 ZM

Einsam

sparkles

Tunnel View Sunset

More Photos

Thanks for the Visit…आने का शुक्रिया

website statistics

Rumbling of Mind

  • Sushil Kumar has won his second Olympic medal, become the first Indian to win two individual Olympic medals for India. Respect. 4 years ago
  • Satyamev Jayate-A great initiative by Aamir Khan & company....must watch on star plus/DD National every Sunday 11:00 AM 4 years ago
  • @sujeetkant number is changed..sent on your linkedin.. 4 years ago
  • @sujeetkant dude I m fine..howz life at you end ..long time 4 years ago
  • In real bad mood.. :( Banged my car today.. 4 years ago

Aaate-Jaate..आते-जाते

Top Clicks..पसंदीदा

  • None

The Selected One’s..चुनिन्दा

U come from..

%d bloggers like this: