Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2052
  • Last Modified:

Problem reading XML response in firefox - 'documentElement has no properties'

I really hope someone can help me cos I'm tearing my hair out here!

I have some javascript that works fine in internet explorer (using the ActiveXObject("Microsoft.XMLDOM") object), but fails in firefox (using the document.implementation.createDocument method).

I am sending an XML request to an asp page, which in turn returns some xml based on the user input. This in itself is working fine, as I can print the contents of this response to the screen in an alert box (see the var xmlLocation).

When I call the objXmlDom.load(xmlLocation) function, a true value is returned, suggesting that the method has run successfully. However, further down, the following error is caught:

TypeError: objXmlDom.documentElement has no properties

Where am I going wrong? One thing I wanted to double check is can I use XML text in the objXmlDom.load() method in mozilla like I can with internet explorer? If not, could you illustrate some code, because I cannot find it anywhere? All the other examples I have seen reference .xml pages directly.  

The XML Response and code are as follows:

<Response><Location>Eaglescliff</Location><Location>East Kilbride</Location><Location>East Wittering</Location></Response>

function DisplayLocation(Input)
{
  if ((Input == null) | (Input.length<1))
    return;

  // GET XML RESPONSE BASED ON INPUT -- THIS WORKS
  var xmlLocation = GetLocation(Input);
 
  // THIS BIT WORKS! (e.g. using IE) -->
  if (window.ActiveXObject)
  {
    var objXmlDom = new ActiveXObject("Microsoft.XMLDOM");
    if (!objXmlDom.loadXML(xmlLocation))
    {
      var sErr = "Response XML String is messed up\n" + xmlLocation;
      alert(sErr);
    }
    var linktext="", count=""
    var nodes = objXmlDom.selectNodes("/Response/Location");            
    for (var i = 0; i < nodes.length; i++)
    {
      linktext += '<a href="javascript:EnterLocation(\''+nodes[i].text+'\');">'+nodes[i].text+'</a><br>';                  
    }
    document.getElementById('locations').innerHTML=linktext;
    nodes = objXmlDom.selectNodes("/Response/Count");            
    count = nodes[0].text;                  
    document.getElementById('count').innerHTML='('+count+' Location(s) Found)';
  }
  // THIS BIT FAILS (e.g. using Firefox) -->
  else if (document.implementation && document.implementation.createDocument)
  {
    try
    {
      var objXmlDom = document.implementation.createDocument("","",null);
   
      if (!objXmlDom.load(xmlLocation))
      {
        var sErr = "Response XML String is messed up\n" + xmlLocation;
        alert(sErr);
        //No error displayed here
      }
      objXmlDom.async=false;
      alert(objXmlDom.documentElement.getElementsByTagName("Location")[0]); // SEEMS TO FAIL HERE             
    }
    catch(e)
    {
      alert(e);
    }  // end try      
  } // end browser check
}  // end function
0
MancAndy
Asked:
MancAndy
  • 6
  • 4
1 Solution
 
wranlonCommented:
Are you able to load the XML file referenced by xmlLocation directly in FireFox?
Is the Web Server sending the correct content type for the XML file (ie: text/xml)?

Try moving the .async property above the call to load the document.  I t hink the default in Mozilla is for async to be true, and you're probably loading the location asynchronously.
0
 
MancAndyAuthor Commented:
Yes I can print the contents of xmlLocation in firefox in an alert box.

I am sending the correct content type in asp...
e.g. I am using Response.ContentType="text/xml"

You're right about the async property being set to true, as I moved this above the load call, and when it was set to false the load error was being caught (e.g. where it says 'Response XML String is messed up')

It is still not working though! It still fails in the same place....
0
 
MancAndyAuthor Commented:
I've just done another test:- When I put the contents of the response to an XML file 'Location.xml', it works successfully.

My main problem is I want to load the XML directly from the xml response. Does anyone have the code to do this.. I can't find it!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wranlonCommented:
I tried your client code, and as long as I moved the async above the load call, it worked fine.

I meant for you to check what happens when you point Firefox directly at xmlLocation; ie: http://localhost/MyXmlAsp.asp   In IE you should see an auto formatted XML tree.  Do you see the same in Firefox?

0
 
MancAndyAuthor Commented:
I don't actually see the formatted tree in IE (like I have seen before), but I do in firefox.

This is the actual XML I get:
<?xml version="1.0" encoding="iso-8859-1"?><Response><Location>Eaglescliffe</Location><Location>East Kilbride</Location><Location>East Sheen</Location><Location>East Sussex</Location><Location>East Wittering</Location><Location>Eastcote</Location><Location>Eastfield</Location><Location>Eastwood</Location><Location>Eastwood</Location><Location>Eaton Socon</Location><Count>42</Count></Response>

I have issued a get request using winsock, and the content-type does indeed come up as 'text/xml'.
0
 
MancAndyAuthor Commented:
This is my ASP code that produces the response if it's any help:

<!--#include virtual="/includes/dsn.asp"-->
<% Response.Expires=0 %>
<%
Function CreateResponse(Location)

  dim cnnDB, rstDB, strSQL, locations
  Set cnnDB=Server.CreateObject("ADODB.Connection")
  Set rstDB=Server.CreateObject("ADODB.Recordset")
  strSQL="SELECT Name FROM Locations WHERE name LIKE '"&Location&"%'"
  cnnDB.Open(db_conn_str)
  rstDB.Open strSQL, cnnDB, 1, 1
 
  'Create the xml response
  responseXML="<?xml version=""1.0"" encoding=""iso-8859-1""?>"
  responseXML=responseXML&"<Response>"
  If not rstDB.EOF Then
    While Not rstDB.EOF And rstDB.AbsolutePosition<11
        responseXML=responseXML&"<Location>"&rstDB("Name")&"</Location>"           
      rstDB.Movenext
    Wend  
  End If
  responseXML=responseXML&"<Count>"&rstDB.RecordCount&"</Count>"
  responseXML=responseXML&"</Response>"

  'Close and kill connection and recordset objects
  rstDB.Close
  Set rstDB=nothing
  cnnDB.Close
  Set cnnDB=nothing
   
  CreateResponse=responseXML
End Function

Function ProcessRequest()

      Dim objXmlDOM
      Dim objRoot
      Dim szXmlRequest
      Dim sErrMsg
      Dim Location
      Dim responseXML

      Set objXmlDOM = Server.CreateObject("Microsoft.XMLDOM")
      szXmlRequest = Request.Form

      If not objXmlDOM.loadXML(szXmlRequest) Then
            sErrMsg = "Invalid XML Request String" + szXmlRequest
            Response.Write(sErrMsg)
            Exit Function
      End If
            
      Set objRoot = objXmlDOM.selectSingleNode("/RequestLocation")
      if IsNull(objRoot) Then
            sErrMsg = "Invalid XML Request String(RequestLocation node not found)" + szXmlRequest
            Response.Write(sErrMsg)
            Exit Function
      End If

      Location = objRoot.getAttribute("Location")
      if ((IsNull(Location)) Or (Location="")) Then
            sErrMsg = "No Location specified" + szXmlRequest
            Response.Write(sErrMsg)
            Exit Function
      End If

      Response.ContentType="text/xml"
      Response.Write(CreateResponse(Location))
      Set objXmlDOM = nothing

End Function

ProcessRequest()
%>
0
 
wranlonCommented:
I tried your asp (sans db query) with your script, and don't have a  problem with it.  

Are you accessing the page from the filesystem, or http?  Is the page on the same server and same port as the ASP page?

What you can try to do is see what the resulting response is with the serializer:
var sText = new XMLSerializer().serializeToString(objXmlDom);
alert(sText);

Also, check what the documentElement is of the result: alert(objXmlDom.documentElement.nodeName)

Or, you can try using XMLHTTPRequest to verify the text value of the XML that is being received:

var oRequest = new XMLHttpRequest();
oRequest.open("GET",xmlLocation,false);
oRequest.send(null);
// the text result:
var sText = oRequest.responseText;
// the xml result, for reference
var oXml = oRequest.responseXML;

0
 
MancAndyAuthor Commented:
Thanks for all your help so far by the way.  

I am issuing the http request to the ASP page from another bit of javascript. found here (this includes the javascript that reads the response):
http://www.just4aviation.net/vacancies/locationsearch.js

When I use the following, a blank alert box is displayed:
var sText = new XMLSerializer().serializeToString(objXmlDom);
alert(sText);

When I use 'alert(objXmlDom.documentElement.nodeName)', I get TypeError: objXmlDom.documentElement has no properties (error being caught).

With the following, when I print out sText as an alert, I get the 'page cannot be displayed' web page code. When I print out oXml as an alert, I get 'null'.

var oRequest = new XMLHttpRequest();
oRequest.open("GET",xmlLocation,false);
oRequest.send(null);
// the text result:
var sText = oRequest.responseText;
// the xml result, for reference
var oXml = oRequest.responseXML;

It seems that the objXmlDom.load function is not working for whatever reason. It does however work when I take the xml from the response, and save it as an xml page that can be read directly.


0
 
wranlonCommented:
I'm assuming you meant: http://www.just4aviation.net/javascript/locationsearch.js

Ok, a couple things.  First, in GetLocation, you can return responseXML for the XML object, and not mess around with having to try to parse the results since they've already been parsed.  objHTTP.responseXML should not be null.  If it is, you have a problem with the resulting XML from the server, and responseText should tell you what those results were.  You can use the load method, but you should specify the URI to the XML, not the text contents of the XML you want to parse (see following parse example).

Second, if you do want to parse XML in mozilla, then you'll need to do the following:

var xmlLocation = GetLocation(Input);
...
var oParser = new DOMParser();
var oXml = oParser.parseFromString(xmlLocation, "text/xml");

In your example, xmlLocation is going to be invalid because it isn't a URI, it's the XML contents from GetLocation, which is already using XMLHttepRequest to fetch the URI.

Assuming you always want GetLocation to return an XML document, then just change responseText to responseXML, and xmlLocation should be your valid XML document.  If you do want GetLocation to continue returning text, then change the Mozilla code to use the above DOMParser example.
0
 
MancAndyAuthor Commented:
Thank you! You've been a great help... I should easily get this working now  :-)

I thought something was up with the load method but I wasn't too sure what to use instead.

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now