Jump to content
Sign in to follow this  
erezes

XML pointer

Recommended Posts

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

Share this post


Link to post
Share on other sites

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);

Share this post


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

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


Link to post
Share on other sites

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);

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