This discussion is archived
7 Replies Latest reply: Mar 17, 2013 3:45 AM by EJP RSS

Working with local xml file

996544 Newbie
Currently Being Moderated
Hi, I would like to work with a local xml file, but I don't want to point to a strict location as I would like my application to run on a few different machines.

Is there a special place within Netbeans project structure that I can place such files?

Say if I try and load a file "somefile.xml" - where is it going to look first?


I have a <default package> with my settings.xml in there. How can I reference this within my app?

Edited by: 993541 on Mar 16, 2013 9:25 AM
  • 1. Re: Working with local xml file
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    993541 wrote:
    Is there a special place within Netbeans project structure that I can place such files?
    dunno Netbeans project structure at all but I'd sugest maven project structure wich is widely used: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
    There you would place such a file in the <tt>main/resources</tt> folder
    Say if I try and load a file "somefile.xml" - where is it going to look first?
    I'ts going to look for it where you tell your program to.
    When created as <tt>new File("somefile.xml")</tt> it will bee searched in the <i>currend working directory</i>. The problem with that is that it is unreliable what this will be at runtime (once your Program left your IDE...).

    You should better get it via <tt>getClass().getResource("somefile.xml")</tt> But in this case the file must be present in the classpath in the same package as the Class aquireing it. Adding a <tt>'/'</tt> in front of the file name expects it in the root directory of a classpath entry.
    I have a <default package> with my settings.xml in there. How can I reference this within my app?
    <tt>getClass().getResource("/settings.xml")</tt>
    But in case you include it into the delivery jar file it will not be writable. Also you cannot expect the installation folder of your App to be writable. On startup of your program you should copy your (default) settings to a writable place like <tt>new File(System.getProperty("user.home2),".myApp/settings.xml");</tt> and modify it there.

    bye
    TPD
  • 2. Re: Working with local xml file
    gimbal2 Guru
    Currently Being Moderated
    TPD Opitz-Consulting com wrote:
    You should better get it via <tt>getClass().getResource("somefile.xml")</tt> But in this case the file must be present in the classpath in the same package as the Class aquireing it. Adding a <tt>'/'</tt> in front of the file name expects it in the root directory of a classpath entry.
    (I would just use getClass().getClassLoader().getResourceAsStream() to not have to worry about relativity of paths)
  • 3. Re: Working with local xml file
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    gimbal2 wrote:
    (I would just use getClass().getClassLoader().getResourceAsStream() to not have to worry about relativity of paths)
    That's a good idea if you know the absolute path of the desired file.

    bye
    TPD
  • 4. Re: Working with local xml file
    gimbal2 Guru
    Currently Being Moderated
    TPD Opitz-Consulting com wrote:
    gimbal2 wrote:
    (I would just use getClass().getClassLoader().getResourceAsStream() to not have to worry about relativity of paths)
    That's a good idea if you know the absolute path of the desired file.
    Its an interesting comment. I shall now spend some time thinking of a way you would not know and not be able to find out.
  • 5. Re: Working with local xml file
    EJP Guru
    Currently Being Moderated
    The only plausible way I can think of is that you just forget where in the project you put it.
  • 6. Re: Working with local xml file
    gimbal2 Guru
    Currently Being Moderated
    EJP wrote:
    The only plausible way I can think of is that you just forget where in the project you put it.
    Close, but no cigar. You can still use a search function to find it.
  • 7. Re: Working with local xml file
    EJP Guru
    Currently Being Moderated
    Ehhhh ... Would you believe it if I said I forgot what the file is a called?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points