0

Hi everyone i'm trying to parse xml files using java using code below..

try{

        DocumentBuilderFactory docFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder docBulider=docFactory.newDocumentBuilder();
        Document config_doc=docBulider.parse("config/appconfig.xml");
        config_doc.getDocumentElement().normalize();
       Node n =config_doc.getDocumentElement();
      NodeList list= n.getChildNodes();
      for(int i=0;i<list.getLength();i++){
          System.out.print(list.item(i));
        if(list.item(i).getNodeName().equalsIgnoreCase("version-name")){
         name=list.item(i).getNodeValue();
        }

      }

        }
        catch (Exception e){e.printStackTrace();}

My file layout is like this

File Layout

I keep getting file not found exception. I also used

getClass().getResouce("config/appconfig.xml").toExternalForm()

and i tried to read as stream too.

Thank you :)

1
  • Re: File not found. Where is the file? Inside your classpath? Or relative to the current working directory? Commented Sep 14, 2012 at 2:05

3 Answers 3

1

this one seems good

getClass().getResouce("config/appconfig.xml").toExternalForm()

except that it is main/config/appconfig.xml

Sign up to request clarification or add additional context in comments.

1 Comment

Or rather /main/config/appconfig.xml
1

Try using

Sytsem.out.println(System.getProperty("user.dir"));

This will print out your program's current working directory, then you can figure out the relative path to your appconfig.xml file.

Good luck

Piers

1 Comment

I think problem is because file is being archieved inside Jar file
0

You can give the absolute path as :

Document config_doc=docBulider.parse(new File("$Absolute Path of the file"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.