文件內容以下java
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/"> <string>浙江</string> <string>杭州</string> <string>58457</string> <string>58457.jpg</string> <string>2016-3-22 17:57:12</string> <string>11℃/16℃</string> <string>3月22日 陣雨</string> <string>東南風微風</string> <string>3.gif</string> <string>3.gif</string> <string>今日天氣實況:氣溫:13℃;風向/風力:南風 1級;溼度:81%;紫外線強度:弱。空氣質量:良。</string> <string> 紫外線指數:弱,輻射較弱,塗擦SPF12-1五、PA+護膚品。 感冒指數:較易發,天涼,溼度大,較易感冒。 穿衣指數:較冷,建議着厚外套加毛衣等服裝。 洗車指數:不宜,有雨,雨水和泥水會弄髒愛車。 運動指數:較不宜,有降水,推薦您在室內進行休閒運動。 空氣污染指數:良,氣象條件有利於空氣污染物擴散。 </string> <string>6℃/12℃</string> <string>3月23日 陣雨轉陰</string> <string>東北風3-4級</string> <string>3.gif</string> <string>2.gif</string> <string>5℃/13℃</string> <string>3月24日 多雲</string> <string>北風3-4級</string> <string>1.gif</string> <string>1.gif</string> <string> 杭州市是浙江省省會,國務院肯定的全國重點風景旅遊城市和歷史文化名城,位於北緯30°16’、東經120°12’,地處長江三角洲南翼,杭州灣西端,錢塘江下游,京杭大運河南端,東瀕杭州灣、錢塘江,南與金華市、衢州市、紹興市相接,西與安徽省黃山市交界,北與湖州市、嘉興市相鄰。下轄8個區、5個縣(市),全市總面積16596平方公里,其中市區面積3068平方公里。改革開放以來,杭州經濟發展迅猛,2007年全市實現國內生產總值4103.89億元,人均GDP達8063美圓,連續17年保持2位數增加,連續4年被譽爲「中國最具幸福感的城市」。 杭州尤以西湖秀麗迷人的天然風光聞名於世。美麗的西湖三面環山,一面瀕城,兩堤臥波,三島浮水,風景秀麗,四季異色,古蹟珠連,名人薈萃,歷代詩人吟詠不絕。杭州天然景觀和人文景觀十分豐富,文物、古蹟衆多,古代庭、園、樓、閣、塔、寺、泉、壑、石窟、摩崖碑刻遍及,衆多景點或詭異神祕、內蘊深沉,或珠簾玉帶、煙柳畫橋,或萬千姿態、蔚然奇觀,或山青水秀、風情萬般。全市現有60多個對外開放景點和40多處重點文物保護單位,以靈隱寺、六和塔、飛來峯、岳廟、西泠印社、三潭印月、花港觀魚、龍井、虎跑等最爲著名。 </string> </ArrayOfString> <!-- 根據城市或地區名稱查詢得到將來三天內天氣狀況、如今的天氣實況、天氣和生活指數 調用方法以下:輸入參數:theCityName = 城市中文名稱(國外城市可用英文)或城市代碼(不輸入默認爲上海市),如:上海 或 58367,若有城市名稱重複請使用城市代碼查詢(可經過 getSupportCity 或 getSupportDataSet 得到);返回數據: 一個一維數組 String(22),共有23個元素。 String(0) 到 String(4):省份,城市,城市代碼,城市圖片名稱,最後更新時間。String(5) 到 String(11):當天的 氣溫,概況,風向和風力,天氣趨勢開始圖片名稱(如下稱:圖標一),天氣趨勢結束圖片名稱(如下稱:圖標二),如今的天氣實況,天氣和生活指數。String(12) 到 String(16):次日的 氣溫,概況,風向和風力,圖標一,圖標二。String(17) 到 String(21):第三天的 氣溫,概況,風向和風力,圖標一,圖標二。String(22) 被查詢的城市或地區的介紹 下載天氣圖標(包含大、中、小尺寸) 天氣圖例說明 調用此天氣預報Web Services實例下載 (VB ASP.net 2.0)-->
注意,該xml是帶命名空間的!!!node
若是這份xml是一份本地文件,能夠用如下方法:web
@Test public void convertTest(){ try { //File directory = new File("");//設定爲當前文件夾 //directory.getCanonicalPath() SAXReader reader = new SAXReader(); Document document = reader.read(new File( "src\\test\\java\\com\\hikvision\\ibms\\indexconfig\\runtime\\engine" + "\\weather.xml")); //法一 String t=document.asXML(); System.out.println("讀取完畢:"+"\n"+t); Element rootElm = document.getRootElement(); System.out.println(rootElm.getName()); List nodes = rootElm.elements("string"); System.out.println("第一種方法讀元素string的個數爲:"+nodes.size()); //法二 HashMap map = new HashMap(); map.put("design",rootElm.getNamespaceURI()); // 建立解析路徑,就是在普通的解析路徑前加上map裏的key值 XPath xPath = document.createXPath("//design:ArrayOfString/design:string"); xPath.setNamespaceURIs(map); List nodes2 = xPath.selectNodes(document); System.out.println("第二種方法讀取元素string的個數爲:"+nodes2.size()); } catch (Exception e) { e.printStackTrace(); } }
若是是基於webservice,遠程讀取文件的話,可使用:數組
package endpoint; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * 類做用調用webservice獲得天氣預報服務 */ public class Weather { /** * 獲取soap請求頭,並替換其中的標誌符號爲用戶的輸入符號 * @param city 用戶輸入城市名 * @return 用戶將要發送給服務器的soap請求 */ private static String getSoapRequest(String city) { StringBuilder sb = new StringBuilder(); sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body> <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">" + "<theCityName>" + city + "</theCityName> </getWeatherbyCityName>" + "</soap:Body></soap:Envelope>"); return sb.toString(); } /** * 用戶把SOAP請求發送給服務器端,並返回服務器點返回的輸入流 * @param city 用戶輸入的城市名稱 * @return 服務器端返回的輸入流,供客戶端讀取 * @throws Exception */ public static InputStream getSoapInputStream(String city) throws Exception { try { String soap = getSoapRequest(city); if (soap == null) { return null; } URL url = new URL( "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"); URLConnection conn = url.openConnection(); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", Integer.toString(soap .length())); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName"); OutputStream os = conn.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8"); osw.write(soap); osw.flush(); osw.close(); InputStream is = conn.getInputStream(); //System.out.println(is.toString()); return is; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 經過dom4j對服務器端返回的XML進行解析 * @param city 用戶輸入的城市名稱 * @return 符串 用,分割 */ public static String getWeather(String city) { Document document=null; SAXReader reader = new SAXReader(); String s=""; Map map=new HashMap(); map.put("design", "http://WebXml.com.cn/"); reader.getDocumentFactory().setXPathNamespaceURIs(map); try { InputStream is = getSoapInputStream(city);//獲得輸入流 document=reader.read(is);//將輸入流轉化爲document String t=document.asXML(); } catch (Exception e) { e.printStackTrace(); } List nodes = document.selectNodes("//design:string"); for (Iterator it = nodes.iterator(); it.hasNext();) { Element elm = (Element) it.next(); String text=elm.getText(); //System.out.println("fsffs"+text); s=s+elm.getText()+"\n"; } return s; } /** * 測試函數 * @param args */ public static void main(String args[]){ Weather w=new Weather(); System.out.println(w.getWeather("杭州")); } }