Q
qiyisoft
Unregistered / Unconfirmed
GUEST, unregistred user!
小弟我最近作了一个Class读取XML文件的类 FactoryClassMaping.java,
目录结构如下:
C:/JavaAPP/FactoryClassMaping.java ->Java源代码
/factory_class_mapping.xml ->要读取的Xml文件
/sql/FactoryClassMaping.class ->编以后的Class文件,放入了Sql包
=============================================
FactoryClassMaping.java 源代码如下
package sql;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.util.*;
public class FactoryClassMaping{
private static FactoryClassMaping fcm = new FactoryClassMaping();
private HashMap factories = null;
private String xmlfile = "factory_class_mapping.xml";
private FactoryClassMaping(){
DocumentBuilderFactory dbf =do
cumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Documentdo
c = null;
try{
builder = dbf.newDocumentBuilder();
doc = builder.parse(xmlfile);
}
catch(Exception e){
e.printStackTrace();
}
try{
Element root =do
c.getDocumentElement();
NodeList nodes = root.getElementsByTagName("factory");
int count = nodes.getLength();
factories = new HashMap(count);
// System.out.println(count);
for(int i = 0 ;
i < count ;
i++){
Element node = (Element)nodes.item(i);
String name = node.getAttribute("name");
String value = ((Text)node.getFirstChild()).getNodeValue();
// System.out.println(value);
factories.put(name , value);
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static FactoryClassMaping getInstance(){
return fcm;
}
public String getClassName(String name){
String classname = "";
// System.out.println(factories.size());
if(factories.containsKey(name)){
classname = (String)factories.get(name);
}
return classname ;
}
public static void main(String[] args){
FactoryClassMaping fc = FactoryClassMaping.getInstance();
System.out.println(fc.getClassName("ConnectionProvider"));
}
}
以上代码在jdk1.4下编译通过,运行无异常
============================================================
在代码中我使用了xmlfile 变量保存了factory_class_mapping.xml相对路径
private String xmlfile = "factory_class_mapping.xml";
结果在部署到Tomcat的时候就犯难了
我将编译好的 FactoryClassMaping.class 放在
E:/Tomcat 4.1/WEBAPPS/ROOT/WEB-INF/classes/sql/FactoryClassMaping.class
但我无论把factory_class_mapping.xml放在哪个目录下,在运行这个类的时候Tomcat都会因为FactoryClassMaping.class 找不到 xml文件而触发异常
在这里请问懂Tomcat的大哥,我应该把xml文件放在那个目录下FactoryClassMaping.class找到xml文件阿
目录结构如下:
C:/JavaAPP/FactoryClassMaping.java ->Java源代码
/factory_class_mapping.xml ->要读取的Xml文件
/sql/FactoryClassMaping.class ->编以后的Class文件,放入了Sql包
=============================================
FactoryClassMaping.java 源代码如下
package sql;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.util.*;
public class FactoryClassMaping{
private static FactoryClassMaping fcm = new FactoryClassMaping();
private HashMap factories = null;
private String xmlfile = "factory_class_mapping.xml";
private FactoryClassMaping(){
DocumentBuilderFactory dbf =do
cumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Documentdo
c = null;
try{
builder = dbf.newDocumentBuilder();
doc = builder.parse(xmlfile);
}
catch(Exception e){
e.printStackTrace();
}
try{
Element root =do
c.getDocumentElement();
NodeList nodes = root.getElementsByTagName("factory");
int count = nodes.getLength();
factories = new HashMap(count);
// System.out.println(count);
for(int i = 0 ;
i < count ;
i++){
Element node = (Element)nodes.item(i);
String name = node.getAttribute("name");
String value = ((Text)node.getFirstChild()).getNodeValue();
// System.out.println(value);
factories.put(name , value);
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static FactoryClassMaping getInstance(){
return fcm;
}
public String getClassName(String name){
String classname = "";
// System.out.println(factories.size());
if(factories.containsKey(name)){
classname = (String)factories.get(name);
}
return classname ;
}
public static void main(String[] args){
FactoryClassMaping fc = FactoryClassMaping.getInstance();
System.out.println(fc.getClassName("ConnectionProvider"));
}
}
以上代码在jdk1.4下编译通过,运行无异常
============================================================
在代码中我使用了xmlfile 变量保存了factory_class_mapping.xml相对路径
private String xmlfile = "factory_class_mapping.xml";
结果在部署到Tomcat的时候就犯难了
我将编译好的 FactoryClassMaping.class 放在
E:/Tomcat 4.1/WEBAPPS/ROOT/WEB-INF/classes/sql/FactoryClassMaping.class
但我无论把factory_class_mapping.xml放在哪个目录下,在运行这个类的时候Tomcat都会因为FactoryClassMaping.class 找不到 xml文件而触发异常
在这里请问懂Tomcat的大哥,我应该把xml文件放在那个目录下FactoryClassMaping.class找到xml文件阿