This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes responseXML.documentElement is null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "responseXML.documentElement is null" Watch "responseXML.documentElement is null" New topic
Author

responseXML.documentElement is null

pradeep chellappan rm
Greenhorn

Joined: Mar 21, 2010
Posts: 15
Hi All,

I am trying to get the contents of XML( from myXml.xml in my local system) and display the same in HTML in the onLoad event. But i am getting a JS error - ""Unable to get property 'getAttribute' of undefined or null reference "" in line number 38 of stateChanged method.

Can you please let me know how to resolve this. Please find the programs below


Thanks in advance.


Main Program:



myXml.xml
-------------


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

That's usually a sign that the content type header in the response is not correctly set.
pradeep chellappan rm
Greenhorn

Joined: Mar 21, 2010
Posts: 15
Hi Bear,

I have given the content-type as below

<meta http-equiv="content-type" content="text/xml; charset=UTF-8">

But this is not working. I am not sure what different header type is expected to fix the issue. Please advise.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

The content type of the page is moot. What's the content type of the XML response?
pradeep chellappan rm
Greenhorn

Joined: Mar 21, 2010
Posts: 15
Bear,

Thanks for your response. I am sorry to say that i am lacking understanding on content Type. I googled to find out, and majority of the cases it is in servlet, the content type is set. But this program is not actually hitting any server, but considers the local machine as server. So, the content type of response xml has to be set in javascript, which i am not sure how to do it. If you can assist me with this, it will be really helpful.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

Even if you are hitting the local server, the XML is being served by HTTP. You should be able to use the debugging tools in any modern browser to look at the network HTTP info and see what the content type is being set to.
pradeep chellappan rm
Greenhorn

Joined: Mar 21, 2010
Posts: 15
Hi Bear,

I tried with IE9, developer toolbar. In the network tab, i am not seeing any information on Request body, Response headers, Response body. I think your suggestion was to look into these. Please let me know what i am missing?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

I don't have Windows handy so I can't check in IE9. Try the Chrome tools, or Firefox with Firebug installed. Be sure tracking Ajax calls are enabled.

(These are tools you need to know how to use if you are going to be working with web apps.)
Rob Crowther
author
Ranch Hand

Joined: Nov 06, 2012
Posts: 295
    
    5

What URL are you accessing your page on, is it a file:// or http:// address? If it's file:// you may not be able to use Ajax due to browser security restrictions, certainly you wouldn't get any network headers (because there's no network request).


Read my books: Hello! HTML5 & CSS3 | HTML5 in Action | Read my blog
pradeep chellappan rm
Greenhorn

Joined: Mar 21, 2010
Posts: 15
Hi Rob,

I am indeed using file protocol to access the xml file from HTML. This program though is working in Firefox but not in IE & Chrome. In Chrome debugger, the issue was more explicit- Cross origin requests are only supported for HTTP."Do you know why it would run on Firefox & can you please elobrate few points on security, please?

Bear - Thanks a lot for your suggestions.

Fyi, I happen to try running the program in eclipse and it was working fine.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

The file protocol is not HTTP and so HTTP headers are moot. if you want all this to work, you'll need to use HTTP.
pradeep chellappan rm
Greenhorn

Joined: Mar 21, 2010
Posts: 15
Bear,

I was not understanding the meaning of the word "moot". Your clarification on that now makes sense. Thanks a lot.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60738
    
  65

In other words, irrelevant.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: responseXML.documentElement is null
 
Similar Threads
Can AJAX helps me to do that
calling function define in a script embeded in xslt from another xslt file
Notify Users before session expires
Java Dynamic Servlet
Filter Option on drop down menu