Jump to content

XML pointer


erezes

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
Link to comment
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);

Link to comment
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
Link to comment
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.

Link to comment
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
Link to comment
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);

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
×
×
  • Create New...