[英]How can I read file.xml from the same package?
I'm trying to read a Properties.xml file which is include in the same package where my class is. 我正在尝试读取Properties.xml文件,该文件包含在与我的课程相同的程序包中。
I'm using this code to update my xml file with new values: 我正在使用以下代码用新值更新我的xml文件:
So far, just I wanted to read the file: 到目前为止,我只想读取文件:
public void UPdateXML(String filename) throws Exception {
this.filename = filename;
System.out.println("Into update xml class");
try {
InputStream fis=BACENGPropertiesFile.class.getResourceAsStream(filename);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse((InputStream) fis);
System.out.println(fis);
System.out.println(doc);
Node child = doc.getFirstChild();
System.out.println(child);
It seems that the file exist and is being reading as an ImputStream. 似乎文件存在并且正在作为ImputStream读取。 But when I try to parse the file to Document (doc) the value is null.
但是,当我尝试将文件解析为文档(doc)时,该值为null。
This is the exit from the server logs: 这是服务器日志的出口:
Into update xml class
java.io.BufferedInputStream@78355ed4
[#document: null]
[properties: null]
The Properties xml file has the following structure: Properties xml文件具有以下结构:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="dataImport.path">/Users/Admin/Documents/</entry>
</properties>
Could somebody tell me what is wrong? 有人可以告诉我怎么了吗?
There is nothing wrong. 没有任何错误。 Your document is not null : it is printed as:
您的文档不为null :它打印为:
[#document: null]
If it would be null
, this would be printed: 如果为
null
,则将其打印出来:
null
By the way, why don't you load properties via Properties.loadFromXML()
? 顺便说一句,为什么不通过
Properties.loadFromXML()
加载属性?
Properties p = new Properties();
try (InputStream in = BACENGPropertiesFile.class.getResourceAsStream(filename)) {
p.loadFromXML(in);
}
What package do you place the Properties.xml to ? 您将Properties.xml放在哪个包中? if it's under the package of UPdateXML, please change:
如果在UPdateXML软件包中,请更改:
InputStream fis=BACENGPropertiesFile.class.getResourceAsStream(filename);
to 至
InputStream fis=UPdateXML.class.getResourceAsStream(filename);
anyway, What package your xml file locate, what classes under the package you should choose. 无论如何,您的xml文件位于哪个包中,应该选择该包下的哪些类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.