• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

SAX: FileNotFoundException

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody, the following class <URLMappingsXmlDAO> it is supposed to print out all the elements of <whatever.xml> file for testing purposes. It is invoked from a servlet <MyServlet>
<MyServlet> is sitting @ WEB-INF/classes/com/mydomain/controller/
<URLMappingsXmlDAO> @ WEB-INF/classes/com/mydomain/controller/web/
<whatever.xml> @ WEB-INF/
I keep getting a FileNotFoundException, for some reason it cannot find the xml file.
Any suggestions? Thanks in advance,

package com.mydomain.controller.web;
import java.io.FileReader;
import java.io.IOException;
import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.SAXParser;
public class URLMappingsXmlDAO extends DefaultHandler
{

public static void loadDocument(String location) throws SAXException, IOException {
XMLReader xr = new org.apache.xerces.parsers.SAXParser();
URLMappingsXmlDAO handler = new URLMappingsXmlDAO();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
FileReader r = new FileReader(location);
xr.parse(new InputSource(r));
}

}
This is the calling servlet:
package com.mydomain.controller;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.NamingException;
import javax.naming.InitialContext;
import com.mydomain.controller.web.URLMappingsXmlDAO;
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
URLMappingsXmlDAO.loadDocument("/WEB-INF/whatever.xml");
}
catch (org.xml.sax.SAXException ex) {
System.err.println("SAX Driver: " + ex);
}
catch (IOException ex) {
System.err.println("File: " + ex);
}
}
}
[ May 31, 2002: Message edited by: Erick Martinez ]
 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I might be wrong.
But the path that you supplied is relative to the servlet context.
I have a feeling that the servlet/the other class that you invoke w'd have no idea as to where it is located if you pass it that way.
You might want to first open a stream to the resourse using a code similar to this and pass the inputstream to get the work done.
The servlet context w'd be able to get you an access to that file.
[CODE]
String XML_PATH = "/WEB-INF/whatever.xml";
ServletContext context = config.getServletContext();
InputStream xmlStream = context.getResourceAsStream(XML_PATH);
URLMappingsXmlDAO.loadDocument(xmlStream);
[CODE]
Hope it works.
 
Ranch Hand
Posts: 313
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not really relative to the servlet, it's really depends upon how your website/application server is setup. Chances are it's looking at the "root" of your application...
Take a look at how your application is set up and start by putting the xml file in the root of the application. Just try to look up the file using the file name and no additional path infol. After you've confirmed where it's pointing (relative addresssing), you can move the file and adjust the path accordingly. If you catch the exception and print out the message, it should give you a clue about where it's looking for the file.
Regards,
Byron
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic