buitrongkhiem Posted April 15, 2011 Share Posted April 15, 2011 i have a xml document: <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><book category="WEB"><title lang="en">XQuery Kick Start</title><author>James McGovern</author><author>Per Bothner</author><author>Kurt Cagle</author><author>James Linn</author><author>Vaidyanathan Nagarajan</author><year>2003</year><price>49.99</price></book><book category="WEB"><title lang="en">Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price></book></bookstore> and has a javascript code <html><body><script type="text/javascript">if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("GET","books.xml",false);xmlhttp.send();xmlDoc=xmlhttp.responseXML; x=xmlDoc.getElementsByTagName("book");for(i=0;i<x.length;i++) { x[i].setAttribute("edition","first"); }//Output all attribute valuesfor (i=0;i<x.length;i++){document.write("Category: " + x[i].getAttribute('category') + "<br />");document.write("Edition: " + x[i].getAttribute('edition') + "<br />");}</script></body></html> i don't understand 2 sentences:{document.write("Category: " + x.getAttribute('category') + "<br />");document.write("Edition: " + x.getAttribute('edition') + "<br />");}help me!please!!!! Link to comment Share on other sites More sharing options...
birbal Posted April 15, 2011 Share Posted April 15, 2011 x=xmlDoc.getElementsByTagName("book")it will return array of element list by tagn name.x.length is the total number of element in x.for (i=0;i<x.length;i++){document.write("Category: " + x.getAttribute('category') + "<br />");document.write("Edition: " + x.getAttribute('edition') + "<br />");} this loop will start from i=0 and everytime it enters in the loop it will increase by 1. this loop will continue till 'i' is less than the total length of elements. (array index starts from 0. so if number of total elements is 4 the last index will be 4-1=3)everytime it enters in the loop it will print the category and edition. "Category: " + x.getAttribute("category") + "<br />"; everytime it will return the value of attribute category of the element which index is 'i'. '+' is used to concatenate. Link to comment Share on other sites More sharing options...
buitrongkhiem Posted April 15, 2011 Author Share Posted April 15, 2011 it will return array of element list by tagn name.x.length is the total number of element in x.this loop will start from i=0 and everytime it enters in the loop it will increase by 1. this loop will continue till 'i' is less than the total length of elements. (array index starts from 0. so if number of total elements is 4 the last index will be 4-1=3)everytime it enters in the loop it will print the category and edition.everytime it will return the value of attribute category of the element which index is 'i'. '+' is used to concatenate.ya,Thank you so much.Now,i see Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.