Read XML from classLoader

Discussions

Web tier: servlets, JSP, Web frameworks: Read XML from classLoader

  1. Read XML from classLoader (3 messages)

    Hi, I have written a method in java class file which loads a resource and displays it contents as : public void getxml(String filename) { ClassLoader cl = this.getClass().getClassLoader(); InputStream inputStream = cl.getResourceAsStream( filename ); int rd=0; try { while((rd=inputStream.read())!=-1) { System.out.print((char)rd); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } I have a jar comprising of following package structure a/b/c/myxml.xml (which is in a jar file which is in the class-path) when i pass a string as a/b/c/myxml.xml the resource is loaded properly. The problem occurs when i pass a string as /a/b/c/myxml.xml Also when the following method is put into a jsp the string /a/b/c/myxml.xml is loaded without any errors. So is there is any difference in policy of loading in case of web-applications? What should be done so that the string /a/b/c/myxml.xml is loaded from a standalone java application

    Threaded Messages (3)

  2. Re: Read XML from classLoader[ Go to top ]

    As I know,'/path/subpath' will take you to root of the current application, and by some case it will take you to the root of your file system. 'path/subpath' is a relative path to the object which invokes it.
  3. Re: Read XML from classLoader[ Go to top ]

    If your resource is placed relative to your current programm, use relative path dont use /a/b..if its there in the root level, then specify a / before the path, the path will be resolved relative to / or root. Remember in linux, root means /. cheers, http://www.javaicillusion.blogspot.com/
  4. Re: Read XML from classLoader[ Go to top ]

    Thanks for the input guys......... Peace. Sanket Raut