Jump to content
Sign in to follow this  
buitrongkhiem

Create a new attribute

Recommended Posts

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!!!!

Edited by MR.Khiem

Share this post


Link to post
Share on other sites
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. Edited by birbal

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...