Jump to content

Opera and Javascript for XML importing


lucasjay

Recommended Posts

Hi everybody,I've been working my first website, and I came to a section where I wanted to get data from an XML file and display it on the web page. So I followed the XML and XML DOM tutorials, but I've hit a maddening wall, thanks to Opera. xhttp=new XMLHttpRequest(); xhttp.open("GET","books.xml",false); xhttp.send(); xmlDoc=xhttp.responseXML; x=xmlDoc.getElementsByTagName("title"); for (i=0;i

Link to comment
Share on other sites

Hi everybody,I've been working my first website, and I came to a section where I wanted to get data from an XML file and display it on the web page. So I followed the XML and XML DOM tutorials, but I've hit a maddening wall, thanks to Opera. ?script?xhttp=new XMLHttpRequest();xhttp.open("GET","books.xml",false);xhttp.send();xmlDoc=xhttp.responseXML;x=xmlDoc.getElementsByTagName("title"); for (i=0;i?x.length;i++) {="" document.write(x.childnodes[0].nodevalue);="" document.write("?x.length;i++);{ document.write(x.childNodes[0].nodeValue); document.write("?br /?");}?/script? This is the code taken straight from the tutorial, and I've saved "books.xml" from there just to check I wasn't making any of my own errors. It works fine in Safari and displays the book list, but nothing displays in Opera. It doesn't throw up any errors either, but when I swap the for loop in the code to the simpler document.write(x[0].childNodes[0].nodeValue); I get the error in the inspector: Uncaught exception: TypeError: Cannot convert 'x[0]' to object. I've done everything I could think of, but no luck, and I cannot fathom what the problem might be. Please help! Regards,Luke

Link to comment
Share on other sites

I'm confused as to what you've done here:for (i=0;i?x.length;i++) {="" document.write(x.childnodes[0].nodevalue);="" document.write("?x.length;i++);{ The forum has no problem with tags, I'm not sure what you did wrong. You can use the [ CODE][/code] block for your code.

Link to comment
Share on other sites

I don't really understand what's going on. Does it work properly in the w3Schools TryIt editor? Check x.length. Maybe, for some reason, it's not getting any nodes.

Link to comment
Share on other sites

It can't be the length property, as like i said I took away the for loop from the example just to test, and that's how I get opera to display an error, instead of nothing at all. The error is in this line: document.write(x[0].childNodes[0].nodeValue); in that for some reason the node from the xml file, which is simple text "cannot be converted to object". It works in the TryIt editor, and in Safari. I would try more browsers, but for my purposes I'm really mostly interested in Opera. Just to clarify, in the example, and in my code above, there is a simple xml file with some book information, and we want to display a list of all the books in the xml file, hence the for loop. But to test a simpler version, literally all I want to do, is get the title of the first book. So getElementsByTagName("title") gets the title tags, which we store in the variable x. Then the code document.write(x[0].childNodes[0].nodeValue); gets the first title tag (x[0]) then the text inside that tag (childNodes[0]) and returns the nodevalue, which should be the title of the first book in the xml file. x[0], to my understanding, should simply contain the text "title", but 'cannot be converted to object', which i just plain don't get. (Like I said, I'm a beginner). The code works fine in Safari, so theres obviously some element that is incompatible with opera, but I don't know of any alternative methods.

Link to comment
Share on other sites

But I was wondering if the code works in the TryIt editor in Opera. Knowing that will narrow down the possible causes of the problem.

Link to comment
Share on other sites

index.html

 <html><head></head><body><script type="text/javascript"> xhttp=new XMLHttpRequest();  xhttp.open("GET","books.xml",false); xhttp.send(); xmlDoc=xhttp.responseXML;  x=xmlDoc.getElementsByTagName("title");  for (i=0;i<x.length;i++)   {   document.write(x[i].childNodes[0].nodeValue);   document.write("<br />");   }</script></body></html>

books.xml:

<?xml version="1.0" encoding="ISO-8859-1"?><!-- Edited by XMLSpy® --><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>

Link to comment
Share on other sites

If you're opening the file from your local filesystem Internet Explorer won't open the XML file because of security restrictions. I don't know about Opera though. I don't see anything wrong in the code. Try testing it in a server environment. If you don't have a web host, you can download server software for your computer.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...