- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Spring中怎么加載ResourceLoader接口資源
本篇文章給大家分享的是有關(guān)Spring中怎么加載ResourceLoader接口資源,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習,希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著(zhù)小編一起來(lái)看看吧。
一 代碼
package lee;import org.springframework.context.*;import org.springframework.context.support.*;import org.springframework.core.io.Resource;import org.dom4j.*;import org.dom4j.io.*;import java.util.*;public class ResourceLoaderTest{ public static void main(String[] args) throws Exception { // 創(chuàng )建ApplicationContext實(shí)例// ApplicationContext ctx = new// ClassPathXmlApplicationContext("beans.xml"); ApplicationContext ctx = new FileSystemXmlApplicationContext("beans.xml"); Resource res = ctx.getResource("book.xml"); // 獲取該資源的簡(jiǎn)單信息 System.out.println(res.getFilename()); System.out.println(res.getDescription()); // 創(chuàng )建基于SAX的dom4j解析器 SAXReader reader = new SAXReader(); Document doc = reader.read(res.getFile()); // 獲取根元素 Element el = doc.getRootElement(); List l = el.elements(); // 遍歷根元素的全部子元素 for (Iterator it = l.iterator();it.hasNext() ; ) { // 每個(gè)節點(diǎn)都是<書(shū)>節點(diǎn) Element book = (Element)it.next(); List ll = book.elements(); // 遍歷<書(shū)>節點(diǎn)的全部子節點(diǎn) for (Iterator it2 = ll.iterator();it2.hasNext() ; ) { Element eee = (Element)it2.next(); System.out.println(eee.getText()); } } }}
二 資源文件
<?xml version="1.0" encoding="GBK"?><計算機書(shū)籍列表> <書(shū)> <書(shū)名>瘋狂Java講義</書(shū)名> <作者>李剛</作者> </書(shū)> <書(shū)> <書(shū)名>瘋狂iOS講義</書(shū)名> <作者>李剛</作者> </書(shū)></計算機書(shū)籍列表>
三 配置文件
<?xml version="1.0" encoding="GBK"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"></beans>
四 測試結果
book.xmlfile [F:\Mybatis\spring\book.xml]
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站