Professional Documents
Culture Documents
04-XMLDOM (Compatibility Mode)
04-XMLDOM (Compatibility Mode)
Ni dung
XML DOM
Nguyn Hng Phng
Email: phuong.nguyenhong@hust.vn Site: http://is.hut.edu.vn/~phuongnh B mn H thng thng tin Vin Cng ngh thng tin v Truyn thng i hc Bch Khoa H Ni
1
1. DOM l g?
DOM nh ngha cc i tng v thuc tnh ca tt c cc phn t ti liu, v cc phng thc truy cp vo chng.
3 4
XML DOM
L m hnh i tng chun cho XML L mt giao din lp trnh chun cho XML c lp vi nn v ngn ng nh ngha mt chun cho truy cp v thao tc vi ti liu XML
2. Cc nt DOM
Mi th trong mt ti liu XML u l nt!
Ton b ti liu l nt ti liu (document node) Mi phn t XML l nt phn t (element node) Vn bn trong cc phn t XML l nt vn bn (text node) Mi thuc tnh l nt thuc tnh (attribute node) Ch thch cng l nt ch thch (comment node)
5 6
18/02/2013
File book.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
3. Cy nt XML DOM
10
11
12
18/02/2013
Np mt ti liu XML
<!DOCTYPE html> <html> <body> <script> if(window.XMLHttpRequest){ xhttp=new XMLHttpRequest(); } else{ // for IE 5/6 xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET","books.xml",false); xhttp.send(); xmlDoc=xhttp.responseXML; document.write("XML document loaded into an XML DOM Object."); </script> </body> </html>
Np mt chui XML
<!DOCTYPE html> <html> <body> <script> text="<bookstore><book>"; text=text+"<title>Everyday Italian</title>"; text=text+"<author>Giada De Laurentiis</author>"; text=text+"<year>2005</year>"; text=text+"</book></bookstore>"; if(window.DOMParser){ parser=new DOMParser(); xmlDoc=parser.parseFromString(text,"text/xml"); }else{ // Internet Explorer xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(text); } document.write("XML string is loaded into an XML DOM Object"); </script> </body> </html>
15
16
5. Cc hm np XML DOM
V l do an ton, trang web v file XML m n mun np phi nm trn cng server. Hm loadXMLDoc() Hm loadXMLString()
17
18
18/02/2013
Hm loadXMLDoc()
function loadXMLDoc(dname){ if (window.XMLHttpRequest){ xhttp=new XMLHttpRequest(); } else{ xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",dname,false); xhttp.send(); return xhttp.responseXML; }
Hm loadXMLString()
function loadXMLString(txt){ if (window.DOMParser){ parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else{ // Internet Explorer xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async=false; xmlDoc.loadXML(txt); } return xmlDoc; }
21
22
V d:
on m JavaScript ly v on text ca phn t <title> u tin trong books.xml:
txt = xmlDoc.getElementsByTagName("title")[0]. childNodes[0].nodeValue
23
24
18/02/2013
7. Truy cp vo cc nt
C ba cch
s dng phng thc getElementsByTagName() s dng vng lp duyt cy nt nh hng trong cy, s dng mi quan h nt.
V d:
tr v tt c cc phn t <title> di nt x:
x.getElementsByTagName("title");
27
28
V d: vidu1.html
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title"); for (i=0;i<x.length;i++){ document.write(x[i].childNodes[0].nodeValue); document.write("<br>"); } </script> </body> </html>
Cc kiu nt
Thuc tnh documentElement ca ti liu XML l nt gc. Thuc tnh nodeName ca nt l tn ca nt. Thuc tnh nodeType ca nt l kiu ca nt
<!DOCTYPE html> <html> <head> vidu2.html <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); document.write(xmlDoc.documentElement.nodeName); document.write("<br>"); document.write(xmlDoc.documentElement.nodeType); </script> </body> </html>
29
30
18/02/2013
V d: vidu3.html
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++){ if (x[i].nodeType==1){//Process only element nodes (type 1) document.write(x[i].nodeName); document.write("<br />"); } } </script> </body> </html>
31
nh hng quan h nt
<!DOCTYPE html> <html> vidu4.html <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0].childNodes; y=xmlDoc.getElementsByTagName("book")[0].firstChild; for (i=0;i<x.length;i++){ if (y.nodeType==1){//Process only element nodes (type 1) document.write(y.nodeName + "<br>"); } y=y.nextSibling; } </script> </body> </html>
32
Cc thuc tnh nt
Mi nt l mt i tng. i tng c phng thc v thuc tnh. Ba thuc tnh quan trng ca mt nt
nodeName nodeValue nodeType
33
34
V d: vidu5.html
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); document.write(xmlDoc.documentElement.nodeName); </script> </body> </html>
35
36
18/02/2013
V d: vidu6.html
Ly v gi tr nt text ca phn t <title> u tin
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; document.write(txt); </script> </body> </html>
38
37
V d: vidu7.html
Thay i gi tr ca phn t <title> u tin
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking"; x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; txt=x.nodeValue; document.write(txt); </script> </body> </html>
39
40
V d: vidu8.html
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); document.write(xmlDoc.documentElement.nodeName); document.write("<br>"); document.write(xmlDoc.documentElement.nodeType); </script> </body> </html>
41
42
18/02/2013
43
44
vidu9.html
vidu10.html
45
46
10. Duyt cy nt
Duyt cy nt
<head> <script src="loadxmlstring.js"></script> </head> <body> <script> text="<book>"; text=text+"<title>Everyday Italian</title>"; text=text+"<author>Giada De Laurentiis</author>"; text=text+"<year>2005</year>"; text=text+"</book>"; xmlDoc=loadXMLString(text);
47 48
18/02/2013
Duyt cy nt
// documentElement always represents the root node x=xmlDoc.documentElement.childNodes; for (i=0;i<x.length;i++) { document.write(x[i].nodeName); document.write(": "); document.write(x[i].childNodes[0].nodeValue); document.write("<br>"); } </script> </body> vidu11.html </html>
49
50
51
52
nh hng trong cy
Truy cp vo cc nt thng qua mi quan h gia cc nt gi l "nh hng" trong cy. Cc thuc tnh ca nt
parentNode childNodes firstChild lastChild nextSibling previousSibling
53 54
18/02/2013
Nt cha
Tt c cc nt (tr gc) u c chnh xc mt nt cha.
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0]; document.write(x.parentNode.nodeName);
vidu13.html
58
13. Ly v cc gi tr DOM
Thao tc vi cc nt
59 60
10
18/02/2013
Ly v gi tr ca mt phn t
Cc nt phn t khng c gi tr text Text ca mt nt phn t c lu tr trong nt con, gi l nt text. => ly v text ca nt phn t, ta ly gi tr ca nt con (nt text). Phng thc getElementsByTagName() tr v mt danh sch nt cha tt c cc phn t vi tn th theo tham s ca phng thc ny. Trt t cc phn t theo trt t xut hin ca chng trong ti liu Thuc tnh childNodes tr v mt danh sch cc nt con Thuc tnh nodeValue tr v gi tr text ca nt text
61
vidu18.html
62
Ly v gi tr ca mt thuc tnh
Trong DOM, cc thuc tnh l cc nt. Nt ny c gi tr text. ly gi tr text ca thuc tnh, s dng
phng thc getAttribute() tr v mt gi tr thuc tnh thuc tnh nodeValue
xmlDoc=loadXMLDoc("books.xml"); txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang"); document.write(txt);
vidu19.html
vidu20.html
vidu21.html vidu22.html
63
64
Thay i gi tr ca nt text
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="Easy Cooking";
vidu23.html
65
66
11
18/02/2013
67
68
Xa b mt nt phn t
xmlDoc=loadXMLDoc("books.xml"); y=xmlDoc.getElementsByTagName("book")[0]; vidu26.html xmlDoc.documentElement.removeChild(y);
Xa text ca nt text
Xa b nt hin ti
xmlDoc=loadXMLDoc("books.xml"); vidu27.html x=xmlDoc.getElementsByTagName("book")[0]; x.parentNode.removeChild(x);
Xa b nt text
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; y=x.childNodes[0]; vidu28.html x.removeChild(y);
69
70
16. Thay th mt nt
Thay th mt nt phn t Thay th d liu trong mt nt text S dng thuc tnh nodeValue
Thay th mt nt phn t
S dng hm replaceChild()
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.documentElement;
vidu32.html
//create a book element, title element and a text node newNode=xmlDoc.createElement("book"); newTitle=xmlDoc.createElement("title"); newText=xmlDoc.createTextNode("A Notebook"); //add the text node to the title node, newTitle.appendChild(newText); //add the title node to the book node newNode.appendChild(newTitle); y=xmlDoc.getElementsByTagName("book")[0] //replace the first book node with the new node x.replaceChild(newNode,y);
71 72
12
18/02/2013
vidu34.html
vidu33.html
73 74
17. To nt DOM
To mt nt phn t mi To mt nt thuc tnh mi To mt thuc tnh s dng setAttribute() To mt nt text To mt nt CDATA Section To mt nt ch thch
To mt nt phn t mi
S dng phng thc createElement()
xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel);
vidu35.html
75
76
To mt nt thuc tnh mi
S dng phng thc createAttribute()
xmlDoc=loadXMLDoc("books.xml"); newatt=xmlDoc.createAttribute("edition"); newatt.nodeValue="first"; x=xmlDoc.getElementsByTagName("title"); x[0].setAttributeNode(newatt);
vidu37.html vidu36.html
77
78
13
18/02/2013
To mt nt text
S dng phng thc createTextNode()
xmlDoc=loadXMLDoc("books.xml");
To nt CDATA Section
S dng phng thc createCDATASection()
xmlDoc=loadXMLDoc("books.xml"); newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");
x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newCDATA);
vidu39.html vidu38.html
79
80
To nt comment
S dng phng thc createComment()
xmlDoc=loadXMLDoc("books.xml"); newComment=xmlDoc.createComment("Revised March 2008"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newComment);
18. Thm nt
Thm mt nt - appendChild() Chn mt nt insertBefore() Thm mt thuc tnh mi Thm text vo nt text - insertData()
vidu40.html
81
82
Thm mt nt - appendChild()
Dng phng thc ny thm mt nt con vo nt hin ti.
xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel);
Chn mt nt insertBefore()
S dng phng thc ny chn mt nt trc mt nt con xc nh. Phng thc ny hu ch khi quan tm ti v tr ca nt thm vo.
x.insertBefore(newNode,null) v x.appendChild(newNode)
xmlDoc=loadXMLDoc("books.xml"); newNode=xmlDoc.createElement("book");
vidu42.html
vidu41.html
14
18/02/2013
vidu43.html
85
vidu44.html
86
Kt qu: Everyday Italian Harry Potter XQuery Kick Start Learning XML Everyday Italian
87
To mt i tng XMLHttpRequest
Tt c cc tnh duyt hin i u tch hp i
tng XMLHttpRequest to i tng XMLHttpRequest:
xmlhttp=new XMLHttpRequest();
Gi yu cu ti server
S dng phng thc open(), send() ca i tng XMLHttpRequest
xmlhttp.open("GET","xmlhttp_info.txt",true); xmlhttp.send();
Phng thc open(method,url,async) M t method: kiu yu cu l GET hoc POST url: v tr ca file trn server async: true (khng ng b), false (ng b) Gi request off ti server string: ch s dng vi yu cu POST
send(string)
90
15
18/02/2013
ng b hay khng ng b?
Gi yu cu khng ng b, JavaScript khng phi i tr li ca server, m c th:
thc hin cc script khc trong khi i tr li (response) ca server. thu nhn tr li khi tr li sn sng
92
Async=true
Async=false
Khuyn co khng nn s dng. JavaScript s i n khi tr li ca server sn sng, nu server bn hoc chm th ng dng s b dng hoc treo. Ch : khng vit hm onreadystatechange
vidu47.html
vidu48.html
93
94
Server Response
nhn p ng t server, s dng thuc tnh responseText hoc responseXML ca i tng XMLHttpRequest
Thuc tnh responseText responseXML M t Nhn d liu p ng nh l mt xu Nhn d liu p ng nh l d liu XML
vidu49.html
95
96
16
18/02/2013
S kin onreadystatechange
Khi mt yu cu c gi ti server, chng ta mun thc hin mt s hnh ng da trn p ng. S kin onreadystatechange c kch hot mi khi readyState thay i. Thuc tnh readyState nm trng thi ca XMLHttpRequest.
vidu50.html
97 98
vidu51.html
status
99
100
1. Cc kiu nt
Kiu nt M t Biu din mt vn bn hon chnh (nt gc ca cy DOM) Biu din i tng ti liu nm gi mt phn ti liu Kiu nt c th l nt con Element (ti a ch c 1) ProcessingInstruction, Comment, DocumentType Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
Ph lc: Cc kiu nt
Document
DocumentFr agment
DocumentTy pe
17
18/02/2013
EntityRefere nce
Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference Text, EntityReference
CDATASectio Biu din CDATASection n trong ti liu (text khng b kim tra bi trnh kim ng) Comment Biu din mt ch thch
Element
Entity
Attr
Text
Notation
103
104
2. Gi tr tr v
Kiu nt Document DocumentFragment DocumentType EntityReference Element Attr Tn nt tr v #document #document fragment doctype name entity reference name element name attribute name Gi tr nt tr v null null null null null attribute value Kiu nt ProcessingInstruction Comment Text CDATASection Entity Notation Tn nt tr v target #comment #text #cdata-section entity name notation name Gi tr nt tr v content of node comment text content of node content of node null null
105
106
3. Kiu nt cc hng
Kiu nt 1 2 3 4 5 6 Hng ELEMENT_NODE ATTRIBUTE_NODE TEXT_NODE CDATA_SECTION_NODE ENTITY_REFERENCE_NODE ENTITY_NODE Kiu nt 7 8 9 10 11 12 Hng PROCESSING_INSTRUCTION_NODE COMMENT_NODE DOCUMENT_NODE DOCUMENT_TYPE_NODE DOCUMENT_FRAGMENT_NODE NOTATION_NODE
107
108
18
18/02/2013
Li hay p
"V i ch l mt cu chuyn, nn iu cn thit khng phi l n di hay ngn, m l hay hay d"
Seneca
109
110
19