1 / 21

Chapter 6: XML in Server

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. Giảng viên: Ngô Đình Thưởng Khoa Tin học, ĐHSP Đà Nẵng http://ndtfit.brinkster.net 08-11-2010. I. Xử lý dữ liệu XML với ASP. Nhắc lại về việc cài đặt IIS 6.0 hay IIS 7.0

ania
Download Presentation

Chapter 6: XML in Server

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 Giảng viên: Ngô Đình Thưởng Khoa Tin học, ĐHSP Đà Nẵng http://ndtfit.brinkster.net 08-11-2010

  2. I. Xử lý dữ liệu XML với ASP • Nhắc lại về việc cài đặt IIS 6.0 hay IIS 7.0 • - IIS 6.0 được cài đặt trên Windows XP • - IIS 7.0 được cài đặt trên Windows Vista hay Windows 7 • Xem hướng dẫn tại : http://ndtfit.brinkster.net (trang chủ và mục Bài đọc • thêm, lớp ASP) • - Tạo website trên localhost để thử nghiệm • Giả sử : tạo thư mục D:\TestXML • Tạo Virtual Directory TestXML (IIS 6.0), Add Application TestXML( Vista) • với thư mục vật lý D:\TestXML • Lưu tệp XML, XSL, ASP, RSS trong D:\TestXML • - Có thể dùng Adobe Dreamweaver CS4 hay ASP Studio để soạn thảo • mã nguồn (nhớ lưu với code là UTF-8) • URL : localhost/TestXML/tệp_ASP

  3. 2- Cấu trúc một trang ASP • <html> • <head> • <style>…</style> • <script>…</script> • <body> • <% • ‘ Khai báo biến riêng cho trang ASP • ‘ Khai báo thủ tục (SUB) hay hàm (FUNCTION) • ‘ Lời gọi thủ tục • %> • </body> • </html> • Nếu sử dụng ASP Studio thì mẫu trên sẽ tự động mở khi New một trang ASP.

  4. 3- Mô hình DOM và ASP • VBScript sử dụng giao diện DOM để lập trình cho XML, XSLT; do vậy những thuộc tính, phương thức của DOM Core hoàn toàn áp dụng cho trang ASP khi xử lý dữ liệu XML. • Vấn đề 1 : Đọc dữ liệu XML và hiển thị trong Browser. • - Open tệp XML và nạp vào bộ nhớ • dim xmldoc • set xmldoc = Server.CreateObject("Microsoft.XMLDOM") • xmldoc.async = false • xmldoc.load(Server.MapPath(“pathname_xml")) • Truy cập các node của cây XML • Sử dụng các phương thức : getElementsByTagName • selectSingleNode • Sử dụng các thuộc tính… : childNodes[i].childNodes[0].text • tập hợp childNodes --> mảng các Node • - Duyệt qua các phần tử của tập hợp : for .. each .. in ------ next

  5. Đọc tệp XML và gởi dữ liệu cho Client • Đây là một ví dụ đơn giản về sử dụng DOM với VBScript ở Server-Side • Cho tệp NguoiThan.xml • <?xml version="1.0"?> • <DCT> • <Student> • <Name>Trần Văn Phương</Name> • <Address>Trường Sa, Khánh Hòa</Address> • </Student> • <Student> • <Name>Nguyễn Duy Phong</Name> • <Address>Hoàng Sa, Đà Nẵng</Address> • </Student> • <Student> • <Name>Phùng Quý Châu</Name> • <Address>Nơi xa vắng</Address> • </Student> • </DCT>

  6. Tệp Read_XML.asp, (nhắn tìm người thân!) <%@LANGUAGE = VBScript%> <% Session.CodePage=65001 Dim fileXml, xmlDoc, Name, Address, i fileXml = Server.MapPath("NguoiThan.xml") ‘ trả về đường dẫn tuyệt đối của file set xmlDoc = Server.CreateObject("Microsoft.XMLDOM") xmlDoc.Load(fileXml) set Name = xmlDoc.getElementsByTagName("Student/Name") set Address = xmlDoc.getElementsByTagName("Student/Address") i = 0 : strHTML=“” For i = 0 To Name.length - 1 str = str & “Bạn: " & Name(i).Text & "<br>“ str = str & “Trước đây ở tại: " & Address(i).Text & "<br><br>" Next Response.Write strHTML set xmlDoc= Nothing %> DEMO Lưu ý: thứ tự của Name, Address tương ứng với một người

  7. Trong môi trường Server, với ASP ta có thể • 1- Truy cập thông tin trong tệp XML để : • Gởi dòng HTML về cho Client (hiển thị ở trang web) • Tạo tệp XML khác • Append dữ liệu vào CSDL quan hệ • 2- Tạo mới tệp XML (createElement) • 3- Append new Node (appendChild) • 4- Remove Node (removeChild) • 5- Biến đổi tệp XML thành HTML stream với sự kết hợp của XSL • 6- Tạo tệp XML từ dữ liệu CSDL quan hệ, với tên thẻ tùy chọn • Ví dụ sau đây minh họa khả năng 1, với mức độ phức tạp hơn, thường dùng trong lĩnh vực báo chí, quản trị dữ liệu.

  8. Cho tệp Articles.Xml, lưu trữ thông tin các bài báo • <?xml version="1.0" encoding="UTF-8"?> • <?xml-stylesheet type="text/xsl" href="articles.xsl"?> • <articles> • <article id="1" pubdate="23/07/2010"> • <title> Malaysia ra lệnh bắt lãnh tụ đối lập </title> • <summary> Tóm tắt bài báo...</summary> • <story> Nội dung chính của bài báo ...</story> • </article> • <! - - article khác - -> • </articles> • Viết trang ASP để tạo ra các đề mục gồm: (chỉ lấy 3 tin mới nhất) • - Tiêu đề tin (title), dùng làm liên kết để đọc toàn văn (story) • Tóm tắt tin (summary) • Giải pháp : • - Truy cập vào tập hợp title, summary, id của cây XML, thứ tự các phần tử con của các tập hợp trên là tương ứng với một tin.

  9. <% dim alen, i, atitle, asummary, artid, link set xmlDoc=Server.CreateObject("Microsoft.XMLDOM") xmlDoc.async="false“ xmlDoc.load Server.MapPath("articles.xml") set atitle=xmlDoc.getElementsByTagName("title") set asummary=xmlDoc.getElementsByTagName("summary") set artid=xmlDoc.getElementsByTagName("article/@id") alen = atitle.length – 2 ‘ lấy ra 3 tin cuối cùng str=“” for i = alen to atitle.length str = "<b><font color='red' size='2'>“ & atitle.item(i-1).text str = str & "</b></font><br>“ & asummary.item(i-1).text str = str &"<p class='tright'><font size='1'><a target='_new' str = str & 'process.asp?mode=viewdetail&id=" str = str & artid.item(i-1).text & "'>" & "More...</font></a><p/>" response.write(str) str=“” next %> Thủ tục process.asp cho phép xem toàn văn 1 tin với ID được chỉ định qua GET

  10. Tệp Process.asp • <% strMode = Request.QueryString("mode") • strID = Request.QueryString("id") • Select Case strMode • Case "viewdetail" • viewDetail server.MapPath("articles.xml"), server.MapPath("articles.xsl"), strID • End select • Function viewDetail(pathXML, pathXSL, strID) • Dim objXML, objNode, objXSL • set objXML = Server.CreateObject("Microsoft.XMLDOM") • objXML.async = false • objXML.load(pathXML) ' Open the XML database • set objNode = objXML.selectSingleNode("articles/article[@id='" & strID & "']") • set objXSL = Server.CreateObject("Microsoft.XMLDOM") • objXSL.async = false • objXSL.load(pathXSL) ' Open the stylesheet • Response.Write(objNode.transformNode(objXSL)) • End Function • %>

  11. Tệp articles.xsl • <?xml version="1.0"?> • <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> • <xsl:template match="articles/article"> • <html><head> • <title>XML và Nội dung động</title> • <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> • <link rel="stylesheet" type="text/css" href="styles.css"></link> • </head> • <body> • <h2> Bản tin Cao đẳng Công nghệ với XML</h2> • <table> • <tr> • <td> <p">Menu</p></td> • <td> <p><xsl:value-of select="title"/> </p> • <p ><xsl:value-of select="story"/> </p> • </td> • </tr> • </table></body></html> • </xsl:template> • </xsl:stylesheet>

  12. Tổng quan về ví dụ 1 Demo 1- Truy cập mọi nút title, summary. Lấy ra 3 node cuối -> tạo link, in ra 2- Chọn 1 link -> ID -> tạo một node TransformNode với node và template Sinh viên download ví dụ này trong phần Bài thực hành XML và thử nghiệm

  13. Vấn đề 2: Đưa dữ liệu XML trình bày như DataGrid • Tệp Students.xml • <?xml version="1.0" encoding="UTF-8" ?> • <DCT> • <Student> • <Code>08T01</Code> • <FullName>Trần Xuân An</FullName> • <Class>08T01</Class> • <BirthDay>20-10-1990</BirthDay> • <Height>1,76</Height> • <Natality>Quảng Nam</Natality> • </Student> • <! - - Another Student - -> • </DCT> • Ý tưởng : - Truy cập đến tập các nodeStudent • - Các node con của Student ->childNodes(i), i=0->5 • Vì muốn điều khiển như DataGrid nên không áp dụng XSL.

  14. Tệp Index.asp • <% • Session.CodePage=65001 • Option Explicit • Const pageSize = 5 ' <-- mỗi trang có 6 record • Dim index • If IsNumeric(Request("s")) Then ' <-- do các nút Prev, Next điều khiển phân trang • index = CInt(Request("s")) • Else • index=0 • End if • %> • <!-- #include file="showstudents.asp“ --> • <html><head> • <title>Danang Colege of Technology</title> • </head> • <body> <%Call ShowStudents(index,pagesize)%> </body> • </html> • - Index: bắt đầu từ “record” thứ • - PageSize: mỗi trang có bao nhiêu “record”

  15. <% Sub ShowStudents(iStartIndex,iPageSize) Dim iStopIndex Dim iTotalRecs Set xmldoc = Server.CreateObject("Microsoft.XMLDOM") xmldoc.async = false xmldoc.load(Server.MapPath("students.xml")) Set nodeList = xmldoc.documentElement.getElementsByTagName("Student") ‘ Tính toán iStartIndex và iStopIndex như trong kỹ thuật phân trang. Đọc thêm ‘ trên ndtfit.brinkster.net/BaiDocThem.htm If iStartIndex<0 Then iStartIndex=0 End If iStopIndex=iStartIndex+iPageSize ‘ In ra những ‘Record” có vị trí từ iStartIndex -> isStopIndex ‘ Đóng tệp End Sub Demo

  16. Tạo tệp XML từ CSDL quan hệ • ‘ Ở đây sử dụng CSDL Access 2000 cho dễ minh họa, vì chuổi kết nối đơn giản. • Cho CSDL SV.MDB, trong đó chứa table SV. Hãy tạo tệp DCT.XML • Với cấu trúc (khác với cách của VB.NET...): • <?xml version=“1.0”> • <DCT> • <Student> • <! - - lấy tên các field và dữ liệu trong field - -> • </Student> • <! - - sinh viên khác - -> • </DCT>

  17. Ý tưởng :

  18. Tệp DB_to_XML.asp Dim Con, Rs ‘ đọc CSDL và đưa vào biến chuổi chứa dòng XML set con=Server.CreateObject("ADODB.Connection") strcon="Provider=Microsoft.Jet.Oledb.4.0; Data Source=" &_ Server.MapPath("Data/SV.Mdb") Con.Open strCon Set Rs=Server.CreateObject("ADODB.RecordSet") strSQL="Select * from SV" Rs.Open strSQL,Con str="<?xml version='1.0' encoding='utf-8'?>" str=str&"<DCT>" Do Until RS.EOF str=str&"<Student><MaSv>"&RS("Masv")&"</MaSv>" str=str & "<HoTen>"&RS("HoTen")&"</HoTen>" str=str & "<Lop>"&RS("Lop")&"</Lop>" str=str & "<QueQuan>"&RS("QueQuan")&"</QueQuan></Student>" Rs.MoveNext Loop str=str&"</DCT>" Rs.Close Con.Close ‘ tiếp trang sau

  19. ' -------------- Lưu dữ liệu vào tệp XML ---------------------- set xmlDoc=Server.CreateObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.loadXML(str) path=Server.MapPath("Save/SV.xml") xmlDoc.Save(path) Response.Write("Đã tạo xong tệp XML!<hr>") link="<a href=Save/SV.xml>Xem nội dung tệp XML </a> &nbsp;|&nbsp;<a href=transform.asp>Xem nội dung trang web được tạo ra</a>" Response.Write link ‘ chú ý : ở đây sử dụng phương thức loadXML, khác với phương thức ‘ load để mở tệp XML đã tồn tại %> DEMO

  20. Tệp Transform.asp , XML+XSL -> trang web <% Session.CodePage=65001 'Load XML set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = false xml.load(Server.MapPath("Save/SV.xml")) 'Load XSL set xsl = Server.CreateObject("Microsoft.XMLDOM") xsl.async = false xsl.load(Server.MapPath("Data/SV.xsl"))‘ bạn tự viết tệp XSL, không dùng for-raech 'Transform file xml.transformNode(xsl) ‘ trả về XML string Response.Write xml.transformNode(xsl) %> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Danh sach sinh vien</title></head> <body></body></html>

  21. Sinh viên download bài giảng, copy & paste để thử nghiệm lại các ví dụ. Tuần sau: Ứng dụng XML: RSS, SOAP,Ajax Thank you! http://ndtfit.brinkster.net

More Related