Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Open an XML file in tomcat with a NOT servlet class

 
Luigi Quarta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing Tomcat-specific here, so moved to the Servlets forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65227
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your path and the location of the file within the classpath do not match.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Luigi Quarta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok but how can i do this? I've tried the method above without results (the document object is always null)
 
vineet kaushik
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hehe
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Luigi Quarta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic