Results 1 to 1 of 1
Thread: access xml file through factory
- 09-30-2010, 06:29 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 40
- Rep Power
- 0
access xml file through factory
Guys,
I am working on my java project, replaced by existing factory properties file into xml file. now i need to call that xml file from my factory using DOM parser.
I am little confused with how to replace the exisitng factory code to access xml.
These are my codes
My exisitng factory:
------------------
package services;
import utility.*;
import exception.ServiceLoadException;
import org.apache.log4j.*;
public class Factory {
private static Logger logger = Logger.getLogger(Factory.class);
private Factory() {
}
private static Factory factory = new Factory();
public static Factory getInstance() {
return factory;
}
public IService getService(String serviceName) throws ServiceLoadException {
logger.info("Getting service from factory now");
try {
@SuppressWarnings("rawtypes")
Class c = Class.forName(getImplName(serviceName));
return (IService) c.newInstance();
} catch (Exception e) {
throw new ServiceLoadException(serviceName + "not loaded");
}
}
private String getImplName(String serviceName) throws Exception {
java.util.Properties props = new java.util.Properties();
java.io.FileInputStream fis = new java.io.FileInputStream(
"config/properties.txt");
props.load(fis);
fis.close();
return props.getProperty(serviceName);
}
}
My new parser Class:
-----------------------
public class parser {
private static Logger logger = Logger.getLogger(parser.class);
public static void buildDom(){
//create factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try{
//create parser
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(new FileInputStream("services/properties.xml"));
//create DOM tree
Element root = document.getDocumentElement();
System.out.println("root element is:" +root);
String attrib = root.getAttribute("CustomerServiceHibernateImpl");
}catch (Exception e){
logger.info("INFO - cant parse file");
System.err.println("Exception: " + e.getMessage());
e.printStackTrace();
}
}}
My new XML file:
------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-properties PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<ICustomerService> services.CustomerServiceHibernateImpl </ICustomerService>
</hibernate-configuration>
what codes do i need to modify in my factory to access xml file thru parser class? i am new to java, still struggling with all these concepts.
Plzzz help!!!
Similar Threads
-
Getting access denied error while importing file using input type="file" with IE7
By sarang1 in forum Advanced JavaReplies: 6Last Post: 02-10-2011, 09:55 AM -
file access from applet
By Ranu in forum New To JavaReplies: 11Last Post: 07-29-2010, 06:37 AM -
File access
By frenk_castle in forum New To JavaReplies: 5Last Post: 04-30-2010, 12:34 PM -
Unable to access velocity.properties file from jar file
By mjwoodford in forum New To JavaReplies: 0Last Post: 10-09-2009, 01:46 PM -
random file access and GUI
By leiferouis in forum New To JavaReplies: 1Last Post: 02-19-2009, 04:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks