Xml - can't read property of null

Hello. I have this code

	var request;
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest()
	}else{
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	request.open('GET', './xml/txml.xml');
	request.onreadystatechange = function(){
		if((request.readyState===4) && (request.status===200)){
		console.log(request.readyState);
		console.log(request.status);
		console.log(request.responseText);
		
	

		var xmlData = request.responseXML;
		var textOutput = xmlData.getElementsByTagName("bisCard")[0];
		var y = textOutput.childNodes[0];
		alert(y);
		
		}
	}//end request.onreadystatechange
	request.send();

The ready state is 4, status 200. the

console.log(request.responseText);

gives me this in the console

<?xml id="xmldata1" version="1.0" encoding="UTF-8"?>
<bisCard>
	<name>joe Bimbo</name>
	<phone type="mobile">503 123.1234</phone>
	<phone type="work">503 123.1234</phone>
	<phone type="fax">503 123.1234</phone>
	<email>joeBimbo@joeBimbo.com</email>
</bisCard>

but I I get the “Uncaught TypeError: Cannot read property ‘getElementsByTagName’ of null” error.
Why is it not picking up on the data available? Thx

What does this show?

console.log(request.responseXML);
1 Like

null

Is txml.xml returning a
Content-Type: application/xml
header?

Hello @Mittineague this is the entire .xml

<?xml id="xmldata1" version="1.0" encoding="UTF-8"?>
<bisCard>
	<name>joe Bimbo</name>
	<phone type="mobile">503 123.1234</phone>
	<phone type="work">503 123.1234</phone>
	<phone type="fax">503 123.1234</phone>
	<email>joeBimbo@joeBimbo.com</email>
</bisCard>

Yes, that is the text of the file.
What I’m talking about is the HTTP header.

Most set-ups default to “text/html”

If you aren’t sending the “application/xml” header, then I guessing “text” is used instead of the xml that you want.

weird…this worked.

var request;
	if(window.XMLHttpRequest){
		request = new XMLHttpRequest()
	}else{
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	request.open('GET', './xml/txml.xml');
	request.onreadystatechange = function(){
		if((request.readyState===4) && (request.status===200)){
		console.log(request.readyState);
		console.log(request.status);
		console.log(request.responseXML);
		console.log(request.responseText);

		console.log(request.responseXML.getElementsByTagName('name')[0]);
		var xmlData = request.responseXML;
		var name = xmlData.getElementsByTagName('name')[0];
		
		$("#selectPnl1").append(name);
		}
	}//end request.onreadystatechange
	request.send();


I finally got a result & it show on the .php page. No header there requiring the application/xml. Frankly puzzled (but don’t call me frank).

Hmmm. Shirley you must joking :stuck_out_tongue_winking_eye:

I had a look at what my localhost httpd.conf had for “types” it uses the “mime.types” file, and that does have application/xml xml in it.

I’m guessing the “alert” I hadn’t noticed before had something to do with the problem.

1 Like

Good catch on the Shirley (that was a fun movie.Surpassed by young frankenstein of course.)
so recon it was a typo? Glad it works now. Thx

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.