[jdom-interest] testing for existance of xml file

Noam Tamim noamt at yahoo.com
Tue Jun 25 08:34:45 PDT 2002


--- Paul Phillips <paul at partitura.com> wrote:
> try {
> 		SAXBuilder builder = new SAXBuilder();
> 		Document aDocument = builder.build(new 
> File("/usr/local/tomcat/myfile2.xml"));
> 		Element rootElement = aDocument.getRootElement();
> 		Element childElement = rootElement.getChild("childElement");
> 		result = childElement.getText();
> 	} catch(JDOMException e) {
> 
> 	} catch(NullPointerException e) {
> 
> 	}
> 
> What is the best way to test for the existance of myfile2.xml.  If it 
> doesn't exist, I want to create it.  should I test to see if aDocument is 
> null?

You can either ask the File for its existence manually (with the 
File.exists() method), before calling build(), or wait for build() 
to throw an IOException (it will throw that exception when it finds
that myfile2.xml does not exist). 

Both ways are possible, however.



=====
Noam.

__________________________________________________
Do You Yahoo!?
Yahoo! - Official partner of 2002 FIFA World Cup
http://fifaworldcup.yahoo.com



More information about the jdom-interest mailing list