導航:首頁 > 編程語言 > 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文件路徑相關的資料

熱點內容
3d文件轉cad 瀏覽:439
有網路力為什麼上不了網 瀏覽:570
手機熱插拔配置文件 瀏覽:716
js打開攝像頭chrome 瀏覽:537
索尼錄音筆文件名 瀏覽:394
ticwatch版本 瀏覽:959
哪裡有日本論文資料庫 瀏覽:450
編程思維訓練有哪些 瀏覽:832
隔空偷文件手機軟體 瀏覽:108
xp遠程桌面連接用戶名密碼 瀏覽:511
iphone4屏幕太暖 瀏覽:962
電腦回收站里的文件怎麼單獨刪除 瀏覽:219
兩張圖片怎麼合成pdf文件 瀏覽:160
服裝專員需要做哪些數據 瀏覽:274
衛衣qq頭像 瀏覽:232
解壓文件怎麼添加圖片 瀏覽:195
表格如何篩選出重復數據數量 瀏覽:496
北京軟體工程師教程 瀏覽:421
voms導出的文件在哪裡 瀏覽:504
maccp文件夾 瀏覽:466

友情鏈接