Avatar of SheppardDigital
SheppardDigital

asked on 

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?
Java

Avatar of undefined
Last Comment
SheppardDigital
ASKER CERTIFIED SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of rrz
rrz
Flag of United States of America image

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

Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

There is no need to create a File object

You can do it either way, File or InputStream. Both equally valid.
Avatar of SheppardDigital
SheppardDigital

ASKER

I tried this and it worked perfectly.

Thanks
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo