using external javascript to load xml document

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am learning xml. I just tried to load xml from external javascript. I just followed the tutorials in w3schools.
but I can not load it. I can not find my mistake.
I have created an external xml document and named it "books.xml"
html document name "html_practice.xml" and external javascript named "loadxmldoc.js".
I can not understand why the file is not loaded.
i am sending the documents with this thread.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; pre<span style="color:Blue; >///books.xml
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; UTF-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; bookstore<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; book <span style="color:Red; category<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; cooking<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; title <span style="color:Red; lang<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; en<span style="color:Black; "<span style="color:Blue; >Everyday Italian<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >Giada De Laurentiis<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; year<span style="color:Blue; >2005<span style="color:Blue; </<span style="color:#A31515; year<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; price<span style="color:Blue; >30.00<span style="color:Blue; </<span style="color:#A31515; price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; book<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; book <span style="color:Red; category<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; children<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; title <span style="color:Red; lang<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; en<span style="color:Black; "<span style="color:Blue; >Harry Potter<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >J K. Rowling<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; year<span style="color:Blue; >2005<span style="color:Blue; </<span style="color:#A31515; year<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; price<span style="color:Blue; >29.99<span style="color:Blue; </<span style="color:#A31515; price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; book<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; book <span style="color:Red; category<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; web<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; title <span style="color:Red; lang<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; en<span style="color:Black; "<span style="color:Blue; >XQuery Kick Start<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >James McGovern<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >Per Bothner<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >Kurt Cagle<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >James Linn<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >Vaidyanathan Nagarajan<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; year<span style="color:Blue; >2003<span style="color:Blue; </<span style="color:#A31515; year<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; price<span style="color:Blue; >49.99<span style="color:Blue; </<span style="color:#A31515; price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; book<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; book <span style="color:Red; category<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; web<span style="color:Black; " <span style="color:Red; cover<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; paperback<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; title <span style="color:Red; lang<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; en<span style="color:Black; "<span style="color:Blue; >Learning XML<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; author<span style="color:Blue; >Erik T. Ray<span style="color:Blue; </<span style="color:#A31515; author<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; year<span style="color:Blue; >2003<span style="color:Blue; </<span style="color:#A31515; year<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; price<span style="color:Blue; >39.95<span style="color:Blue; </<span style="color:#A31515; price<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; book<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; bookstore<span style="color:Blue; >
**********************************

/////html_practice.html


<span style="color:Blue; <!<span style="color:#A31515; DOCTYPE <span style="color:Red; html <span style="color:Red; PUBLIC <span style="color:Black; "<span style="color:Blue; -//W3C//DTD XHTML 1.0 Transitional//EN<span style="color:Black; " <span style="color:Black; "<span style="color:Blue; http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; html <span style="color:Red; xmlns<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://www.w3.org/1999/xhtml<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; head<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; meta <span style="color:Red; http-equiv<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Content-Type<span style="color:Black; " <span style="color:Red; content<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; text/html; charset=utf-8<span style="color:Black; " <span style="color:Blue; />
<span style="color:Blue; <<span style="color:#A31515; script <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; text/javascript<span style="color:Black; " <span style="color:Red; src<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; loadxmldoc.js<span style="color:Black; "<span style="color:Blue; >

<span style="color:Blue; <<span style="color:#A31515; title<span style="color:Blue; >xmlpractice<span style="color:Blue; </<span style="color:#A31515; title<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; head<span style="color:Blue; >

<span style="color:Blue; <<span style="color:#A31515; body<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; script <span style="color:Red; type<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; text/javascript<span style="color:Black; "<span style="color:Blue; >

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
document.write(x[2].childNodes[0].nodeValue);

<span style="color:Blue; </<span style="color:#A31515; script<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; body<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; html<span style="color:Blue; >
*******************************

////loadxmldoc.js

function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}
[/code]
<br/>[/code]



View the full article
 
Back
Top