Jump to content

Create a new attribute


buitrongkhiem
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...