从 JAVA 项目中读取 xml 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16141360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read xml file from within the JAVA project
提问by user1427961
I need to know where I should place the xml file inside the Java project. I need to access the placed xml file and parse it. I have tried using resources folder but its not working out for me. Can someone suggest me a good idea on how to access the xml file which is placed within the JAVA project. I am using Eclipse IDE.
我需要知道我应该将 xml 文件放在 Java 项目中的什么位置。我需要访问放置的 xml 文件并解析它。我曾尝试使用资源文件夹,但它对我不起作用。有人可以就如何访问位于 JAVA 项目中的 xml 文件向我建议一个好主意。我正在使用 Eclipse IDE。
采纳答案by mki
You can use the structure of maven : Project/src/main/resources
可以使用maven的结构:Project/src/main/resources
I suppose it is a data xml file. If it is a config file a better way is to access it via the classpath. This is a common place where to put the resources, but you can choose your own directory. If you have problem to open the file, it is your path which is not correct. Try first to open it with a absolute path. Something like d:/workspace/Project/src/main/resources/file.xml. It will works.
我想它是一个数据xml文件。如果它是一个配置文件,更好的方法是通过类路径访问它。这是放置资源的常见位置,但您可以选择自己的目录。如果您无法打开文件,则是您的路径不正确。首先尝试用绝对路径打开它。类似于 d:/workspace/Project/src/main/resources/file.xml。它会起作用。
回答by NPKR
use like below class --
像下面的课程一样使用-
this.getClass().getClassLoader().getResourceAsStream("path of file");
NOTE : this xml file structure in project
注意:项目中的这个 xml 文件结构
+pro
+src
+resource
-test.xml
Example:
例子:
public class ReadFile {
public void testReadFile() {
InputStream in = this.getClass().getClassLoader()
.getResourceAsStream("/Resources/tset.xml");
//praparing DOM
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
// by sending input stream as input to DOM
Document doc = docBuilder.parse(in);
}
public static void main(String[] args) {
ReadFile file = new ReadFile();
file.testReadFile();
}
}
回答by N..
resources folder is the correct folder to place your xml file. If it is not finding the file in that folder, then it may not be on classpath. So, add this folder to your project's classpath.
resources 文件夹是放置 xml 文件的正确文件夹。如果未在该文件夹中找到该文件,则它可能不在类路径上。因此,将此文件夹添加到项目的类路径中。
Go to Project -> Properties -> Java Build Path -> Source -> Add Folder
转到项目 -> 属性 -> Java 构建路径 -> 源 -> 添加文件夹
Then add the resources folder to your classpath. Thats all...You are good to go..
然后将资源文件夹添加到您的类路径。就是这样......你很高兴去......
回答by Vitaly
You need to make sure that Eclipse includes /resources folder into the application classpath.
您需要确保 Eclipse 将 /resources 文件夹包含到应用程序类路径中。
Open your launcher from Run Configurations
, goto Classpath
tab and if it's not there - add it.
从 中打开您的启动器Run Configurations
,转到Classpath
选项卡,如果它不存在 - 添加它。
The file can be read with this.getClass().getClassLoader().getResource*
methods.
可以使用this.getClass().getClassLoader().getResource*
方法读取文件。