Jump to content

Trouble Loading an XML file in my template.


htmlnoobie3344
 Share

Recommended Posts

i was reading the XML tutorial on w3c (good tutorials, BTW) and followed the one where it tells you how to load an XML file. I tried to write a javascript with 2 functions, one that gets the XMLrequest and returns it, and one that loads the XML file and i've tried to use them but it doesn't work, so obviously i did SOMETHING wrong :-p anyways, here's my code:the Javascript code:

function getXMLRequest(){	var xhttp;	if(window.XMLHttpRequest)	{		xhttp = new XMLHttpRequest();	}	else	{		xhttp=new ActiveXObject("Microsoft.XMLHTTP");	}	return xhttp;}function LoadXMLDoc(str){	var xhttp = getXMLRequest();	var xmlDoc = new DOMParser();	xhttp.open("GET",str,false);	xhttp.send("");	xmlDoc=xhttp.responseXML;}

the script to load it into an HTML:

<!--Include the JS script to access the XML object--><script type="text/javascript" src="../Javascripts/XML_Loader.js">function LoadXMLintoMemory(){			LoadXMLDoc("../XMLs/A-Z list.xml");		alert("XML loaded");	}</script><BODY onLoad="LoadXMLintoMemory()">

and the XML file:

<A-Z_list>	<C>		<Cayetano_Santos_Godino>			<name>Cayetano Santos Godino</name>				<dob>		<!--Date Of Birth-->					<month>October</month>					<day>31</day>					<year>1896</year>				</dob>								<died>	<!--Describes when he died.-->					<month>November</month>					<day>15</day>					<year>1944</year>				</died>		</Cayetano_Santos_Godino>	</C></A-Z_list>

look forward to input from this wonderful community :-)

Link to comment
Share on other sites

PLEASE in future posts do not write "doesn't work." That doesn't help at all. Please do write what behavior you expected, what actually happened, and what (if anything) you tried to do to fix it.Please also test your projects in a browser that has a useful error console. Firefox has a very friendly Error Console, which you can access in the Tools menu. It's always on, so it fills up with junk from other people's pages. Your errors will appear at the end. To simplify using it, open it BEFORE you load your page and press the Clear button. This empties the window. Now load your page and run your code. If there is a syntax or runtime error, it will be reported.You should also test for errors in your xhttp object.

Edited by Deirdre's Dad
Link to comment
Share on other sites

PLEASE in future posts do not write "doesn't work." That doesn't help at all. Please do write what behavior you expected, what actually happened, and what (if anything) you tried to do to fix it.Please also test your projects in a browser that has a useful error console. Firefox has a very friendly Error Console, which you can access in the Tools menu. It's always on, so it fills up with junk from other people's pages. Your errors will appear at the end. To simplify using it, open it BEFORE you load your page and press the Clear button. This empties the window. Now load your page and run your code. If there is a syntax or runtime error, it will be reported.You should also test for errors in your xhttp object.
Well, i expected the alert box to tell me it loaded the file, i'll try your way, thanks for your patience, sorry about this :-pEDIT: Tried what you suggested and used FF to get errors. for some reason it was saying that "loadXMLintoMemory()" wasn't defined. I got rid of the function and just left it as is, still nothing, i keep checking in FF's error console and no errors come up Edited by Rosario Corsini
Link to comment
Share on other sites

I notice when you quoted the error, you changed the case of "loadXMLintoMemory". Are you certain the case is the same in all places?Did you copy and paste this function from the web somewhere? That sometimes introduces invisible characters that mess things up. If nothing else works, try retyping the entire "loadXMLintoMemory" function from scratch.

Link to comment
Share on other sites

I notice when you quoted the error, you changed the case of "loadXMLintoMemory". Are you certain the case is the same in all places?Did you copy and paste this function from the web somewhere? That sometimes introduces invisible characters that mess things up. If nothing else works, try retyping the entire "loadXMLintoMemory" function from scratch.
I used FF's erro console to evaluate some code i wrote in the doc it self, and it worked, spit out my message, BUT, it returned this:
Error: uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.open]"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame :: java script:%20var%20xhttp;%20if(window.XMLHttpRequest)%20{%20xhttp%20=%20new%20XMLHttpRequest();%20if(!xhttp)%20{%20alert("Could%20not%20get:%20"%20+%20xhttp);%20}%20else%20{%20alert("this%20was%20a%20triumph,%20i'm%20making%20a%20note%20here");%20}%20}%20else%20{%20xhttp%20=%20new%20ActiveXObject("Microsoft.XMLHTTP");%20}%20var%20xmlDoc;%20xhttp.open("GET",%20"../XMLs/A-Z_list.xml",false);%20xhttp.send("");%20xmlDoc=xhttp.responseXML; :: <TOP_LEVEL> :: line 1"  data: no]

......i have no idea what the heck it's telling me :-p

Link to comment
Share on other sites

Your code works fine for me.On your end, it seems to break when you call xhttp.open()I suspect that xhttp is not a valid ajax object. Insert a line of code as follows:

var xhttp = getXMLRequest(); // your existing linealert(xhttp); // my addition

In Firefox, you should get this message:

[object XMLHttpRequest]

If not, then something is wrong with getXMLRequest. Try retyping it.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Your code works fine for me.On your end, it seems to break when you call xhttp.open()I suspect that xhttp is not a valid ajax object. Insert a line of code as follows:
var xhttp = getXMLRequest(); // your existing linealert(xhttp); // my addition

In Firefox, you should get this message:

[object XMLHttpRequest]

If not, then something is wrong with getXMLRequest. Try retyping it.

Ok,i got it to load, tried to assign the value from it to an element didn't work they have diffrent tags one is XML_TRIAL (in the main doc) and another is C + all the child nodes, do the IDs in the HTML doc have to be the same as the tags in the XML?
Link to comment
Share on other sites

If I understand the question, HTML id attributes and XML element names have nothing to do with each other. In fact, once you have received an XML document through AJAX, it shouldn't interact with HTML directly at all. JavaScript makes all the connections.Please post the code you are using to access nodes in your XML.

Link to comment
Share on other sites

If I understand the question, HTML id attributes and XML element names have nothing to do with each other. In fact, once you have received an XML document through AJAX, it shouldn't interact with HTML directly at all. JavaScript makes all the connections.Please post the code you are using to access nodes in your XML.
	var XML_to_El_Trial = document.getElementById("XML_TRIAL");	if(XML_to_El_Trial)	{		alert("would you kindly....");		XML_to_El_Trial.value = XMLDoc.getElementByTagName("A-Z_List")[0].childNodes[0].nodeValue;	}

I so love that phrase.....

Link to comment
Share on other sites

Try going for the element's innerHTML instead, like:

XML_to_El_Trial.innerHTML = XMLDoc.getElementByTagName("A-Z_List")[0].childNodes[0].nodeValue;

unless of course XML_to_El_Trial is actually an HTML <input/> element.Also, keep in mind that IE and other browsers greatly different in the way they traverse XML's childNodes. IE doesn't include whitespace nodes, and other browsers do. With that in mind, I'd suggest you go for the element with (for example):

XMLDoc.getElementByTagName("name")[0].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
 Share

×
×
  • Create New...