在我的项目中读取xml文件(src) [英] Read xml file inside my project (src)
问题描述
我有2个关于GWT的问题:
我正在开发一个应用程序,它必须读取项目内部的xml文件,但我把这个:
Document doc = docBuilder.parse(new File(stringPath));
成为stringPath:
URL路径= nameClase.class.getProtectionDomain()。getCodeSource()。getLocation();
String stringPath = path.toString();
stringPath = stringPath.substring(5);
int num = stringPath.indexOf(classes);
stringPath = stringPath.substring(0,num + 8);
stringPath + =namePackage / nameFile.xml;
通过这个,我得到了它的xml文件路径。如果我在我的本地机器上这样做是好的,但是当我做一个.war文件时,我只选择了src文件夹,之后用ant文件创建了我的.war文件,然后当我在tomcat中部署我的gwt项目时,我没有做到这一点,因为捕获的路线是:
machine / war / WEB-INF / classes / package / nameFile.xml
但是当我做war文件时,我不会获取war / WEB-INF文件夹,因为我确实.jar src文件夹。
如何获取xml文件的路径?此xml文件位于src文件夹内。
另一个问题是如何创建日志文件?有人有一个例子,我可以看到它吗?
谢谢。
try
InputStream is = getClass()。getResourceAsStream(/ namePackage / nameFile.xml);
Document doc = docBuilder.parse(is);
I have 2 questions about GWT:
I'm developing a app and it have to read a xml file that is inside project, but I put this:
Document doc = docBuilder.parse(new File(stringPath));
being stringPath:
URL path = nameClase.class.getProtectionDomain().getCodeSource().getLocation();
String stringPath = path.toString();
stringPath = stringPath.substring(5);
int num = stringPath.indexOf("classes");
stringPath = stringPath.substring(0, num + 8);
stringPath += "namePackage/nameFile.xml";
With this I get the route where It´s xml file. If I do this in my local machine is ok, but when I do a .war file only I select src folder and after with ant file I create my .war file, then when I deploy my gwt project in tomcat I don´t get do it ok, because the route that catch is:
machine/war/WEB-INF/classes/package/nameFile.xml
But when I do war file I don´t obtain the war/WEB-INF folder because I do a .jar the src folder.
How can I get the path to the xml file? This xml file is inside src folder.
The other question that I have is how do I create a log file? somebody have a example where I can see it?
Thanks.
try
InputStream is = getClass().getResourceAsStream("/namePackage/nameFile.xml");
Document doc = docBuilder.parse(is);
这篇关于在我的项目中读取xml文件(src)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!