1 / 18

Chapter 6: XML in Server (tiếp theo)

1- Xử lý dữ liệu XML với ASP 2- Ứng dụng RSS 3- Ajax với XML, ASP 4- SOAP. Chapter 6: XML in Server (tiếp theo). Giảng viên: Ngô Đình Thưởng Khoa Tin học, ĐHSP Đà Nẵng http://ndtfit.brinkster.net 08-11-2010. I- RSS. 1- RSS là gì ?

greta
Download Presentation

Chapter 6: XML in Server (tiếp theo)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1- Xử lý dữ liệu XML với ASP 2- Ứng dụng RSS 3- Ajax với XML, ASP 4- SOAP Chapter 6: XML in Server (tiếp theo) Giảng viên: Ngô Đình Thưởng Khoa Tin học, ĐHSP Đà Nẵng http://ndtfit.brinkster.net 08-11-2010

  2. I- RSS • 1- RSS là gì ? • RSS là một định dạng tập tin thuộc họ XML dùng trong việc chia sẻ tin tức Web (Web syndication) được dùng bởi nhiều website tin tức và weblog. Chữ viết tắt (theo tiếng Anh) dùng để chỉ các chuẩn sau: • Rich Site Summary (RSS 0.91) RDF Site Summary (RSS 0.9 and 1.0) Really Simple Syndication (RSS 2.0.0) • RSS là một chủ đề rất nhiều người quan tâm, dễ tham khảo. Trong phần này GV chỉ nêu những vấn đề chính: • Khuôn mẫu một tệp RSS đơn giản. • Xây dựng một trang ASP để đọc RSS, vì là một tệp có cấu trúc như tệp XML nên hoàn tòa có thể sử dụng mô hình DOM để lập trình. • Có thể tham khảo trên: http://www.w3schools.com/rss/default.asp

  3. Cấu trúc tệp RSS (extension: .rss hay.xml, các thẻ là quy định) • <?xml version="1.0" encoding=“utf-8" ?><rss version="2.0"><channel><title>Danang College of Technology</title>  <link>http://www.dct.udn.vn</link>  <description>Technology RSS</description> •   <item>    <title>Science-Technology</title><link>http://vietnamnet.vn/khoahoc/index.rss </link><description>New RSS Science-Technology on VNN</description>  </item> •   <item>    <title>Information Technology</title>    <link>http://www.pcworld.com.vn</link>    <description>PCWorld Viet Nam</description>  </item></channel><! - - Item khác - -></rss>

  4. Tệp Read_RSS.asp (chỉ nêu phần chính) • <% • theFeed = “http://localhost/ASP/RSS/ndtfit.xml” ‘ tên tệp RSS • ‘ có thể dùng phần mở rộng .xml thay cho .rss • Set objXML = Server.CreateObject("Microsoft.XMLDOM") • objXML.Load(theFeed) • set objRoot = objXML.documentElement • set objItems = objRoot.getElementsByTagName("item") • NewsItem=“” • For each objItem in objItems • theTitle = objItem.selectSingleNode("title").Text • theLink = objItem.selectSingleNode("link").Text • theDesc = objItem.selectSingleNode("description").Text • NewsItem= NewsItem & "<a href=" & theLink& ">“ &_ • & theTitle & "</a>" & "<br />“ & theDesc & "<br /><hr />" • Response.Write(NewsItem) ‘ In một News • Next • set objItems = Nothing : set objXML = Nothing • %> • Demo

  5. II-Ajax = Javascript+HTML+XML 1- Ajax là gì ? AJAX viết tắt của Asynchronous JavaScript and XML Hiện nay kỹ thuật Ajax được ứng dụng rất nhiều trên các trang web nhằm tăng tính thân thiện và tiện lợi hơn cho trang web, nhất là tăng tốc hiển thị trang nhờ không tải lại thành phần cố định. Thành phần cố định Thành phần thay đổi

  6. Giải thích nguyên tắc Sinh viên download bài thực hành từ ndtfit.brinkster.net để nghiên cứu và thử nghiệm các ví dụ trên tệp XML. Sau đó ứng dụng trên một CSDL quan hệ.

  7. Tệp CD_Catalog.xml • <?xml version="1.0" encoding="utf-8"?> • <CATALOG> • <CD> • <TieuDe>Huyền thoại Mẹ </TieuDe> • <CaSi>Cẩm Vân</CaSi> • <Anh>CamVan.jpg</Anh> • <QuocTich>Việt Nam</QuocTich> • <NhaSanXuat>Phương Nam Film</NhaSanXuat> • <DonGia> 160.000 VND </DonGia> • <NamPhatHanh> 1988 </NamPhatHanh> • </CD> • <! - - CD khác  • </CATALOG> • Lưu ý : Lưu trữ hình ảnh ca sĩ bằng tệp tin JPG • Viết ứng dụng tra cứu đĩa CD dựa trên tên ca sĩ

  8. Tệp Index.html • <html><head> • <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"> • <script language = Javascript href = myScript.js></script> • <title>Tim dia CD theo ten Ca si</title></head><body" • <form> • Chọn đĩa CD: • <select onchange="showCD(this.value);"> • <option value="Bob Dylan">Bob Dylan</option> • <option value="Cẩm Vân">Cẩm Vân</option> • <option value="Khánh Ly">Khánh Ly </option> • </select> • </form>8 • <div id="txtHint" "> • <b>Thông tin về CD sẽ hiển thị dưới đây</b> • </div> • </body></html>

  9. Tệp myScript.js} var xmlHttp; function showCD(str){ xmlHttp=GetXmlHttpObject(); // tao đối tượng xmlHttp tùy theo browser if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } var url="getcd.asp“+"?q="+str; // gởi thông tin đến server url=xmlHttp.onreadystatechange=stateChanged; //không có cặp ngoặc tròn xmlHttp.open("GET",url,true); xmlHttp.send(null); } function stateChanged() { if (xmlHttp.readyState==4){ document.getElementById("txtHint").innerHTML=xmlHttp.responseText;} } function GetXmlHttpObject(){ var xmlHttp=null; try { xmlHttp=new XMLHttpRequest(); } // mới nhất catch (e) { try {xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } // mới catch (e) {xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");} // cũ } return xmlHttp; }

  10. Bàn về đối tượng XMLHttpRequest • IE 7+, FireFox, Opera 8.0+, Safari : xmlHttp=new XMLHttpRequest(); • IE 5.0, 6.0 : xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); • Các thuộc tính và phương thức (xem trên w3schools.com) • Thuộc tính : • onreadystatechange • readyState • Phương thức : • open() • send()

  11. Tệp getCD.asp <% singer=request.querystring("q") // nhận thông tin do client gởi set xmlDoc=Server.CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load(Server.MapPath("cd_catalog.xml")) set nodes=xmlDoc.selectNodes("CATALOG/CD[CaSi='" & singer & "']") for each row in nodes // duyệt qua các CD của singer for each cell in row.childnodes // duyệt qua các thông tin của 1 CD ‘ Response.write cell. childNodes(i).Text, i=0->cell.childNodes.lenght ‘ thông tin này sẽ được gởi về client ‘ client nhận và lưu trong xmlHttp.responseText ‘ đưa vào innerText để hiển thị next Next set xmlDoc=nothing %> DEMO

  12. III- SOAP 1- SOAP là gì ? SOAP là viết tắt của Simple Object Access Protocol SOAP là một giao thức truyền thông trên Internet, độc lập nền, có thể vượt qua tường lửa, dùng để gởi thông tin giữa các ứng dụng thông qua việc gởi/nhận các thông điệp được gói dưới dạng XML. (theo w3schools.com/soap/) 2- Cấu trúc gói tin (message) : • Envelope là thành phần bắt buộc, là nút gốc của gói XML, chứa hai phần: • Headerchứa thông tin bắt buộc về một ứng dụng cụ thể (như chứng thực, thanh toán, vv) về thông điệp SOAP • Body: chứa thông điệp SOAP thực tế dành cho các điểm cuối cùng của message • - SOAP HTTP Binding : HTTP + XML = SOAP • Một yêu cầu SOAP có thể là HTTP POST hay HTTP GET

  13. 3- Ví dụ Tệp Request.htm <html><head><title>POST Tester</title> <script language="JavaScript“ src=“myScript.js”> </script> </head> <body> Ứng dụng SOAP<br> Số thứ nhất <input id=numOne name=soThuNhat> </br> Số thứ hai <input id=numTwo name=soThuHai> </br> <input type="button" value="POST" onclick="doPost()"> // trong myScript.js </body> </html>

  14. Phân tích các gói tin sẽ xây dựng dựa trên cấu trúc SOAP • Client gởi yêu cầu : • <goiDi> • <soThuNhat> giá_trị </soThuNhat> • <soThuNhi> giá_trị </soThuNhi> • </goiDi> • - Server gởi kết quả (message) : (ở đây là trang ASP) • Nếu có lỗi: • <Error> • <reason> Lý_do_lỗi </reason> • </Error> • Nếu không lỗi : • <goiDi> • <ketQua> giá_trị </ketQua> • </goiDi>

  15. Tệp MyScript.js function doPost() { var xmlDoc, xhHTTP, sXML sXML = "<goiDi>”; sXML +="<soThuNhat>“+ numOne.value+ "</soThuNhat>”; sXML += “<soThuNhi>“ + numTwo.value+"</soThuNhi>”; sXML += “</goiDi>" xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.loadXML(sXML); xhHTTP = new ActiveXObject("MSXML2.XMLHTTP"); xhHTTP.open("POST", "http://localhost/ASP/SOAP/congHaiSo.asp", false); xhHTTP.setRequestHeader("SOAPAction", "goiDi"); // của client xhHTTP.send(xmlDoc); xmlDoc = xhHTTP.responseXML; if(xmlDoc.documentElement.nodeName == "Error") { alert("Du lieu XML goi di khong dung!");} else { alert("Kết quả: " +xmlDoc.selectSingleNode("/goiDi/ketQua").text);} }

  16. Tệp CongHaiSo.asp <% Response.ContentType = "text/xml" Response.AddHeader "SOAPAction", "ketQuaPhepCong" Set xdRequestXML = Server.CreateObject("Microsoft.XMLDOM") xdRequestXML.load Request sNumber1 = xdRequestXML.selectSingleNode("/goiDi/soThuNhat").text sNumber2 = xdRequestXML.selectSingleNode("/goiDi/soThuNhi").text nResult = CLng(sNumber1) + CLng(sNumber2) If Err.number <> 0 Or Request.ServerVariables("HTTP_SOAPAction") <> "goiDi" Then Response.Write ErrorXML() Else Response.Write SuccessXML(nResult) End If ‘ Tiếp trang sau các hàm ErrorXML và SucessXML

  17. Function ErrorXML() • Dim sXML • sXML = "<Error><Reason>Khong dung du lieu</Reason></Error>" • ErrorXML = sXML • End Function • Function SuccessXML(sResult) • Dim sXML • sXML = "<goiDi>" • sXML = sXML & "<ketQua>" & Cstr(sResult) & "</ketQua>" • sXML = sXML & "</goiDi>" • SuccessXML = sXML • End Function • %> • DEMO

  18. Sinh viên download bài giảng, copy & paste để thử nghiệm lại các ví dụ. Ôn tập thi học kỳ I Đây là chủ đề khó, mong cố gắng Thank you! http://ndtfit.brinkster.net

More Related