简体   繁体   English

从JAVA项目中读取xml文件

[英]Read xml file from within the JAVA project

I need to know where I should place the xml file inside the Java project. 我需要知道将xml文件放在Java项目中的位置。 I need to access the placed xml file and parse it. 我需要访问放置的xml文件并解析它。 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. 有人可以建议我如何访问放置在JAVA项目中的xml文件。 I am using Eclipse IDE. 我正在使用Eclipse IDE。

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();
        }
    }

You can use the structure of maven : Project/src/main/resources 您可以使用maven的结构:Project / src / main / resources

I suppose it is a data xml file. 我想这是一个数据xml文件。 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. 像d:/workspace/Project/src/main/resources/file.xml之类的东西。 It will works. 它会奏效。

resources folder is the correct folder to place your xml file. resources文件夹是放置xml文件的正确文件夹。 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. 因此,将此文件夹添加到项目的类路径中。

Go to Project -> Properties -> Java Build Path -> Source -> Add Folder 转到项目 - >属性 - > Java构建路径 - >源 - >添加文件夹

Then add the resources folder to your classpath. 然后将resources文件夹添加到类路径中。 Thats all...You are good to go.. 多数民众赞成......你很高兴......

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*方法读取该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM