aspose file tools*
The moose likes Servlets and the fly likes Open an XML file in tomcat with a NOT servlet class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Open an XML file in tomcat with a NOT servlet class" Watch "Open an XML file in tomcat with a NOT servlet class" New topic
Author

Open an XML file in tomcat with a NOT servlet class

Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
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 XMLReader java 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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61063
    
  66

Nothing Tomcat-specific here, so moved to the Servlets forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61063
    
  66

Your path and the location of the file within the classpath do not match.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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
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: 41574
    
  54
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.)


Ping & DNS - my free Android networking tools app
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
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: 41574
    
  54
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
Ok but how can i do this? I've tried the method above without results (the document object is always null)
vineet kaushik
Greenhorn

Joined: Aug 05, 2009
Posts: 13
hehe
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Document doc = builder.parse(XMLReader.class.getResourceAsStream("cfg.xml"));

try this...


dunno if it makes any difference but this is how I usually do it.

Sean


I love this place!
Luigi Quarta
Greenhorn

Joined: Jul 30, 2009
Posts: 17
Thank you very much, it seems that like this:



the Document object isn't null, in fact the "Opened" message is printed... anyway the class can't find anything in the document... and if i do:



I get this message: "Document: #null"
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

I get this message: "Document: #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
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: 12769
    
    5
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.

Bill
 
Consider Paul's rocket mass heater.
 
subject: Open an XML file in tomcat with a NOT servlet class