This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
Open an XML file in tomcat with a NOT servlet class
Luigi Quarta
Greenhorn
Joined: Jul 30, 2009
Posts: 17
posted
0
Hello,
I have an xml file placed in /my_app/WEB-INF/classes/data/cfg.xml and i would like to open it with the XMLReaderjava class placed in /my_app/WEB-INF/classes/utility/XMLReader.java. To open the file i do this:
The "doc" object rest null... Can you tell me how open an XML file in Tomcat with a not servlet class?
Thanks in advantage
The directory "/my_app/WEB-INF/classes" is in your classpath. To load that file from the classpath -- which is what getResourceAsStream does -- you would have to specify its path relative to the classpath. In your example that would be "/data/cfg.xml".
Luigi Quarta
Greenhorn
Joined: Jul 30, 2009
Posts: 17
posted
0
I've tried also to like this but the resulting object is always null... this means that the file is not found isn't it?
Maybe my classpath is .../Apache Software Foundation/Tomcat 6.0/bin/ cause when i tried to open the XML file creating a File object that was the path in wich Tomcat looked (without find nothing)
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35220
7
posted
0
Maybe my classpath is .../Apache Software Foundation/Tomcat 6.0/bin/ cause when i tried to open the XML file creating a File object that was the path in wich Tomcat looked (without find nothing)
That is a file path, not a classpath; those concepts are not related. (As an aside, in a web app you always need to use absolute paths for file I/O, because there is no default directory to use.)
My web app should be installed on different machines...use absolute path is quite difficult...
Ulf Dittmer wrote:
As an aside, in a web app you always need to use absolute paths for file I/O, because there is no default directory to use
If there is no default directory and I use an absolute path to a file, in different installations my web application will not work
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35220
7
posted
0
My web app should be installed on different machines...use absolute path is quite difficult...
I was referring to what you wrote earlier: "Maybe my classpath is .../Apache Software Foundation/Tomcat 6.0/bin/ cause when i tried to open the XML file creating a File object that was the path in wich Tomcat looked" which led me to believe that you were using relative paths in a web app, which doesn't work due to the lack of a default directory.
Absolute paths should not be completely fixed - part of them should be the context path. That way it doesn't matter what the directory is named in which the web app is installed - you'll always get the correct one.
Luigi Quarta
Greenhorn
Joined: Jul 30, 2009
Posts: 17
posted
0
Ok but how can i do this? I've tried the method above without results (the document object is always null)
This does not mean that the doc is null. That is just what the default implementtation of doc.toString() will print out.
If you want to see the actual xml then you will need to serialise it.
Sean
Luigi Quarta
Greenhorn
Joined: Jul 30, 2009
Posts: 17
posted
0
Thank you very much with your method all works perfectly! Not i can open XLM files in Tomcat and not ^_^
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
One more thing....
There is a lovely table in the JavaDocs for the org.w3c.dom.Node interface that shows what each kind of Node toString() operation will display plus a bunch of very useful information. The different kinds of Node behave quite differently.
Getting very familiar with that table will help you resolve similar problems in your continued XML adventure.