views:

48

answers:

2

I'd like to parse through XML files with java - ok, easy. It would be REALLY nice if I could use an XML file that I've created within a folder inside my same Eclipse project (let's call it the "resources" folder). Due to issues with version control, multiple development platforms, and general simplicity, it would be VERY, VERY nice to have this capability, rather than accessing the file in the file system.

Is there an easy way to do this, perhaps one that doesn't require downloading/installing additional packages? It's for a school assignment, and the rule is that we can use any library or class that comes with the IDE, but that's it. Using a "nonstandard" library is cheating.

+1  A: 

You can do this already. (If I understand your question.)

If you have an Eclipse project like so:

Project
    src
        java.packages
    resources
        xmlfiles.xml

You can access the xml files like so:

File f = new File("resources/yourXmlFile.xml");

That way you don't have to worry about where the files are exactly on the file system.

Have a look at the StackWrap4J project setup. It is most likely identical to what you want:

my proj setup

jjnguy
Yes, you are understanding my question correctly. Two questions, if you happen to know the answer to them...1) can I just create "resources" as a regular folder? 2) Do you know if this will be compatible with CVS for version control? Thanks.
Ryan
@Ryan, Yeah. Resources can be just a plain old file. In eclipse just right click on the project and there should be an add file or new file option.
jjnguy
@Ryan, if you want, you can add the resources file to version control without a problem.
jjnguy
@Ryan, added a pic of a project setup. It should be exactly what you want.
jjnguy
@Justin I'm surprised you're not recommending the Class.getResource() approach. It's got no disadvantages. But it has obvious advantages, and while they may not be that helpful right now to @Ryan, they will be later. (Like in a web application.) It should be encouraged.
Ladlestein
@Ladle, well for testing it is easiest to have a local resources folder. The get resources option will work well too, but I'm just suggesting what works best for me in most cases. I'm definitely not saying that my way is better than your way btw. There is just more than 1 option.
jjnguy
I appreciate the input that both of you gave - this is very helpful. Thank you so much!
Ryan
+2  A: 

If you tell Eclipse that the "resources" folder should be considered a source folder (right click on it, then the files from it will be on your classpath when you run your application. You can then access them in your Java program like this:

this.getClass().getResouce("/yourfilename.xml");

You don't have to worry about the working directory - you're just accessing a file on the classpath.

Ladlestein