erezes 0 Posted March 13, 2013 Report Share Posted March 13, 2013 (edited) <!DOCTYPE html><html><head><script src="loadxmldoc.js"></script></head><body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]y=x.childNodes[0];document.write(y.nodeValue); </script></body></html> <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" cover="paperback"><title lang="en">Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price> </book> </bookstore> RESULT : Everyday Italian I want to go <author> James Linn </ author>RESULT : James Linn HOW? Edited March 13, 2013 by erezes Quote Link to post Share on other sites
Ingolme 1,021 Posted March 13, 2013 Report Share Posted March 13, 2013 Empty text nodes are still considered text nodes. That includes line breaks between tags. You should use getElementsByTagName() to get the element you want. You also need to access the text node once you have the element. var book = xmlDoc.getElementsByTagName("book")[2]; // Get the book you're looking for (number 2 in the list)var authors = book.getElementsByTagName("author"); // Get a list of authors of the book.var a = authors[3]; // The author you want is number 3 on the listvar name = a.firstChild; // The name is the textnode contained in the <author> elementdocument.write(a.nodeValue); Quote Link to post Share on other sites
erezes 0 Posted March 13, 2013 Author Report Share Posted March 13, 2013 Empty text nodes are still considered text nodes. That includes line breaks between tags. You should use getElementsByTagName() to get the element you want. You also need to access the text node once you have the element. var book = xmlDoc.getElementsByTagName("book")[2]; // Get the book you're looking for (number 2 in the list)var authors = book.getElementsByTagName("author"); // Get a list of authors of the book.var a = authors[3]; // The author you want is number 3 on the listvar name = a.firstChild; // The name is the textnode contained in the <author> elementdocument.write(a.nodeValue); OK why here(next link) dont Successfulhttp://www.w3schools.com/dom/tryit.asp?filename=try_dom_getelementsbytagname Quote Link to post Share on other sites
Ingolme 1,021 Posted March 13, 2013 Report Share Posted March 13, 2013 Because I told it to write a.nodeValue instead of name.nodeValue. A simple mistake, you should look through the code and try to find these kind of mistakes yourself. It's important to understand the code instead of just copying and pasting. Quote Link to post Share on other sites
erezes 0 Posted March 13, 2013 Author Report Share Posted March 13, 2013 (edited) Because I told it to write a.nodeValue instead of name.nodeValue. A simple mistake, you should look through the code and try to find these kind of mistakes yourself. It's important to understand the code instead of just copying and pasting. it is not work !!! believe me with a or name . http://www.w3schools.com/dom/tryit.asp?filename=try_dom_getelementsbytagname Edited March 13, 2013 by erezes Quote Link to post Share on other sites
Ingolme 1,021 Posted March 14, 2013 Report Share Posted March 14, 2013 I tested this code on that page and it works fine: var book = xmlDoc.getElementsByTagName("book")[2]; // Get the book you're looking for (number 2 in the list)var authors = book.getElementsByTagName("author"); // Get a list of authors of the book.var a = authors[3]; // The author you want is number 3 on the listvar name = a.firstChild; // The name is the textnode contained in the <author> elementdocument.write(name.nodeValue); Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.