通過(guò)Spring工具類獲取classpath下的文件資源,解析xml-創(chuàng)新互聯(lián)

File cfgFile = ResourceUtils.getFile("classpath:test.txt");通過(guò)Spring工具類獲取cla
sspath下的文件資源,解析xml

或者

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的寧津網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

org.springframework.core.io.Resource fileRource = new ClassPathResource("test.xml");

獲取文件:fileRource.getFile();

獲取文件流:fileRource.getInputStream();

獲取到xml:進(jìn)行解析:例:<myXml> <template id="index" type="page" name="首頁(yè)" templatePath="test.ftl" staticPath="ii.html"/></myXml>

Document document = new SAXReader().read(fileRource.getFile());

//獲取到myxml下面的template節(jié)點(diǎn)為id為test的節(jié)點(diǎn)
Element element = (Element) document.selectSingleNode("/myXml/template[@id='test']");

//解析xml,并獲取到節(jié)點(diǎn)里的值。

String id = element.attributeValue("id");

//獲取到j(luò)ava的臨時(shí)文件:

System.getProperty("java.io.tmpdir"),

//windows是,unix是/

File.separator

一、簡(jiǎn)單的用ApplicationContext做測(cè)試的話,獲得Spring中定義的Bean實(shí)例(對(duì)象).可以用:

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("RegisterDAO",RegisterDao.class);

在主文件中引入副文件:

<import resource="classpath*:env/*/applicationContext.xml" />

在主文件中可以引入一些properties文件,作為applicationContext中的變量

<context:property-placeholder location="classpath*:env/default/ecp.properties,
classpath*:env/default/common.properties,
classpath*:ecp.properties"
ignore-resource-not-found="true" ignore-unresolvable="true" />

本文名稱:通過(guò)Spring工具類獲取classpath下的文件資源,解析xml-創(chuàng)新互聯(lián)
分享鏈接:http://bm7419.com/article12/ddpggc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)微信小程序、網(wǎng)站維護(hù)網(wǎng)頁(yè)設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)