解析网上的XML文件
生活随笔
收集整理的這篇文章主要介紹了
解析网上的XML文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
第一步:觀察一個網點
http://www.webxml.com.cn/zh_cn/web_services.aspx?offset=1
第二步:用java.net.Urlconnection向服務器發出請求
?? UrlConnection低層是 Socket.
public?class?Demo_Mobile?{@Testpublic?void?test1()?throws?Exception{URL?url?=?new?URL("http://webservice.webxml.com.cn"+?"/WebServices/MobileCodeWS.asmx/"+?"getMobileCodeInfo?mobileCode="+?"186982747585&userID=");//2:獲取連接HttpURLConnection?con?=?(HttpURLConnection)?url.openConnection();//3:設置連接超時時間con.setConnectTimeout(3000);//設置為get請求con.setRequestMethod("GET");//設置可以從服務器讀取數據con.setDoInput(true);//連接服務器con.connect();//判斷連接是否成功int?code?=?con.getResponseCode();if(code==200){//獲取服務器返回的數據InputStream?in?=?con.getInputStream();StringBuilder?sb?=?new?StringBuilder();byte[]?bs?=?new?byte[1024];int?len?=?0;while((len=in.read(bs))!=-1){sb.append(new?String(bs,0,len));} //???System.err.println("返回的結果為:"+sb.toString());//解析SAXReader?sax?=?new?SAXReader();Document?dom?=?sax.read(new?StringReader(sb.toString()));Element?root?=?dom.getRootElement();String?ss?=?root.getText();System.err.println(ss);}//關閉連接con.disconnect();} }轉載于:https://my.oschina.net/u/2311668/blog/466900
總結
以上是生活随笔為你收集整理的解析网上的XML文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pyqt 中__init__(self,
- 下一篇: JQuery下锚点的平滑跳转