导航:首页 > 编程语言 > java获取xml文件路径

java获取xml文件路径

发布时间:2024-04-18 19:43:52

『壹』 web项目中java类怎么访问WebRoot目录下的xml文件,路径怎么写

最坏的使用绝对路径

『贰』 java怎么获取resources下的文件路径

1.读取本地的xml文件,需要注意对应的路径
//读取xml文件,xmlFile为读取文件的路径DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance;DocumentBuilderbuilder=factory.newDocumentBuilder;Documentdocument=builder.parse(xmlFile);NodeListnodeList=document.getElementsByTagName(thisTag);//指定标签()的节点集合for(itni=0;i<nodeList.getLength;i++){//循环获取每个节点信息Nodenode=nodeList.item(i);NamedNodeMapattributes=node.getAttributes;for(intj=0;j<attributes.getLength;j++){Nodeattribute=attributes.item(j);System.out.println(attribute.getNodeName+":"+attribute.getNodeValue);}}
注意:getElementsByTagName方法只是属于document与Element的方法

所以,当针对某个Node查找对应的节点时,需要先强制转换为Element
ElementnodeToElement=(Element)node;NodeListosNodeList=nodeToElement.getElementsByTagName(thisTag);//thisTag为指定标签
2.读取txt文件

一般的数据存储都是键值对的方式在文件中记录,开发人员多是根据已知的键,从文件中取得对应的值。

例如Config.txt中内容为:

name=jack

sex=boy

要从java程序中读取该文件的内容
Fileconfig_file=newFile("./Config");//此处使用相对路径Stringconfig_file_fullpath=config_file.getAbsoluteFile.toString;readConfigconfig=newreadConfig(config_file_fullpath);Stringname=config.get("name");//name为jack//对获取的数据进行处理//...
3.读取.csv文件

csv文件一般为表格,是多行多列的数据,列对应相应不同的属性,java实现逐行读取每列单元格的值。

『叁』 web项目的WebRoot/user/user.xml这个路径,在java里中该如何读取这个文件,路径如何写,请高手指点,多谢

其实这个很简单,我觉得你的问题应该是如何自动得到Webroot外层的本地目录,你把
request.getRealPath("/") 这条语句的结果输出出来应该就知道怎么做了。

『肆』 java获取XML路径提示空指针问题

空指针的位置实在getPath方法,因为getResource方法返回了Null,导致null.getPath就是空指针了,问题出在你没有获取版到note.xml文件,所以你需要解决权这个问题,你把代码拆开写就知道了,getResource 用个变量接受,然后你看它 是不是 null

『伍』 JAVA获取当前文件夹下面所有XML文件怎么弄

import java.io.File;

public class ShowAllXML {
public static void main(String[] args) {
File file = new File("").getAbsoluteFile();
String[] dir;
dir = file.list();
for (int i = 0; i < file.list().length; i++) {
if (dir[i].length() > 4) {
if (dir[i].endsWith(".xml")) {
print(dir[i]);
}
}
}

}

private static void print(String string) {
System.out.println(string);
}
}

『陆』 关于java中xml文件配置的路径问题

嗯,是过滤,在这个里面实现了些什么?
这个问题太大
了,而且这是struts2,更加复杂。在里面做了很多事情,最起码的
至少有加载你的struts.xml配置文件,解析你的配置文件,然后通过你的请求路径,找到相应的action,封装你的表单数据等等,还有很多过滤器。。。struts2不是一两句能说完的,看看源代码吧,如果看得懂,就知道做了什么了

『柒』 Java读取配置文件的几种方法


在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法。
一、读取xml配置文件
(一)新建一个java bean
package chb.demo.vo;
public class HelloBean {
private String helloWorld;
public String getHelloWorld() {
return helloWorld;
}
public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}
}
(二)构造一个配置文件
?xml version="1.0" encoding="UTF-8"?
!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ""
beans
bean id="helloBean" class="chb.demo.vo.HelloBean"
property name="helloWorld"
valueHello!chb!/value
/property
/bean
/beans
(三)读取xml文件
1.利用
ApplicationContext context = new ("beanConfig.xml");
HelloBean helloBean = (HelloBean)context.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
2.利用FileSystemResource读取
Resource rs = new FileSystemResource("D:/software/tomcat/webapps/springWebDemo/WEB-INF/classes/beanConfig.xml");
BeanFactory factory = new XmlBeanFactory(rs);
HelloBean helloBean = (HelloBean)factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。
二、读取properties配置文拿镇件
这里介绍两种启敬技术:利用spring读取properties 文件和利用java.util.Properties读取
(一)利用spring读取properties 文件
我们还利用上面的HelloBean.java文件,构造如下beanConfig.properties文件:
helloBean.class=chb.demo.vo.HelloBean
helloBean.helloWorld=Hello!chb!
属性文件中的"helloBean"名称即是Bean的别名设定,.class用于指定类来消旁粗源。
然后利用org.springframework.beans.factory.support.来读取属性文件
BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
reader = new (reg);
reader.loadBeanDefinitions(new ClassPathResource("beanConfig.properties"));
BeanFactory factory = (BeanFactory)reg;
HelloBean helloBean = (HelloBean)factory.getBean("helloBean");
System.out.println(helloBean.getHelloWorld());
(二)利用java.util.Properties读取属性文件
比如,我们构造一个ipConfig.properties来保存服务器ip地址和端口,如:
ip=192.168.0.1
port=8080
则,我们可以用如下程序来获得服务器配置信息:
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("ipConfig.properties");
Properties p = new Properties();
try {
p.load(inputStream);
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("ip:"+p.getProperty("ip")+",port:"+p.getProperty("port"));

『捌』 java中如何获取WebContent下config文件下的xx.xml文件路径

前者:
用在web.config或者app.config之类.
是系统约定的节点,约定在这个节点下的所有
节点会被system.configuration.configurationmanager.appsetting读到.
后者:
完全的自定义接点,appsettings表示什么意思,add表示什么意思将在自己写的xml解析方法里指定和使用.
简单来说,config是xml的一个子集.
通常的xml都是只定义基本语法,至于节点的层次,节点格式,节点的含义,节点怎么被解析都是你自己定义.使得你的xml文件能和你的xml解析方法对应.
而web.config,app.config这类,是microsoft和软件作者已经定义好了节点意义,你只需要遵守他的格式和规则,就能达到配置作用。
打个可能不太好的比方:
xml文件本身是扑克牌.
config是斗地主。
你用config,就不需要自己制定规则,按照它的规则打就行。很方便,但是你不能违反他的规则。
而你自己写xml,还要先制定好规则,规则怎么定都随便你,然后按照这个规则出牌.
当然,这些都有一个大前提,都满足xml节点规范,你不能制定扑克牌的规则中放入几个麻将牌....

『玖』 java读取配置文件的方法(xml)

用的是jdom包

URL url = RederXml.class.getClassLoader().getResource("");
String path = url.toString() + "/config.xml";\\工程种xml的路径
HashMap<String, String> map = new HashMap<String, String>();
SAXBuilder sax = new SAXBuilder();
Document doc = null;
try {
doc = sax.build(path);
} catch (JDOMException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Element root = doc.getRootElement();

阅读全文

与java获取xml文件路径相关的资料

热点内容
c语言做界面小程序用什么数据库 浏览:609
存货核算模块数据来源于哪里 浏览:647
re文件管理改手机型号 浏览:711
凭证业务文件和凭证内容文件 浏览:552
vi查看文件的编码格式 浏览:322
交换日的女演员 浏览:917
德国进攻法国的电影有哪些 浏览:751
克拉拉限制片 浏览:544
公马 洛基 浏览:304
微博的应用数据在哪个文件夹 浏览:525
我的阿姨1韩剧中文版 浏览:592
女主发明高科技的小说 浏览:22
电脑微信相册文件夹 浏览:20
linuxstrptime 浏览:499
电影世界捉鬼大佬 浏览:588
伦理片爱爱 浏览:166
在usb安装linux 浏览:35
socket5linux 浏览:124
客人结局为什么小孩进洞 浏览:331
eclipse打开app安装包 浏览:33

友情链接