Accessing xml file in filter

Hi,

I've created a filter in my applications default package, and I've also created an xml file in the default package too. I'm trying to load the XML file but I keep getting a message saying the xml file was not found.

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			   DocumentBuilder builder = factory.newDocumentBuilder();
			   Document document = builder.parse(new File("/Configuration.xml"));
			   Element rootElement = document.getDocumentElement();

Open in new window


I'm obviously not accessing the xml file correctly, but I can't seem to figure it out, can someone tell me where I'm going wrong?
SheppardDigitalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
try this:

public class Parser {

    public static void main(final String[] args) throws Exception {

	final Parser parser = new Parser();
	final Document document = parser.parseFile("Configuration.xml");

	final Element rootElement = document.getDocumentElement();
    }

    public Document parseFile(final String filename) throws Exception {

	final DocumentBuilderFactory factory = DocumentBuilderFactory
		.newInstance();
	final DocumentBuilder builder = factory.newDocumentBuilder();

	final File f = new File(getClass().getResource(filename).getFile());

	return builder.parse(f);

    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rrzCommented:
There is no need to create a File object.
import javax.xml.parsers.*;
import org.w3c.dom.*; 
public class AccessTest{ 
    public static void main(String[] args) { 
		try{
                     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	             DocumentBuilder builder = factory.newDocumentBuilder();
		     Document document = builder.parse(AccessTest.class.getResourceAsStream("Configuration.xml"));
		    Element rootElement = document.getDocumentElement();
		    System.out.print(rootElement.getNodeName());
        }catch(Exception e){}			   
    }  
} 

Open in new window

0
gurpsbassiCommented:
There is no need to create a File object

You can do it either way, File or InputStream. Both equally valid.
0
SheppardDigitalAuthor Commented:
I tried this and it worked perfectly.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.