Sunday, 2 September 2012

Loaded XML Document is null (Javascript) - Stack Overflow

I am trying to load an XML document into Javascript so it can be outputted on a website, however when I load the XML document it is null, and so I get an exception each time I try to read from it. Here is my code:

CODE ON THE WEBPAGE

    var xmlDoc;      loadXML();      function loadXML(){         xmlDoc = loadXMLDoc("http://www.tomrichardsonweb.co.uk/ABC/xml/pubs.xml");     }      function loadPub(){          if(xmlDoc != null){              document.getElementById('pub').innerHTML=             xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;         }else{             alert("null");         } 

loadXMLDoc Method

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; 

}

XML file

    < ?xml version="1.0" encoding="UTF-8" ?> <pub>     <name>Bay Horse</name>     <description>Situated at the foot of the stunning Pennine Range, amidst the          breathtaking landscape of Rivington, you will find The Bay Horse Inn. With open fires, cosy corners and a warm friendly atmosphere, this family run inn really does have something for everyone.  The perfect place to relax with family and friends our well stocked bar offers the finest cask ales, refreshing lagers and ciders, quality wines and spirits and a wide selection of soft drinks, teas and coffees.   For diners our chalkboards boast classic pub food, all freshly prepared and cooked to order, alongside a great range of award winning "Pieminister" pies.   And for those visiting the area on business or pleasure, or just passing through on a wider journey, and looking for somewhere to rest their heads then our bed and breakfast rooms could be just the answer!  Our friendly team (and our friendly regulars!) will try to make your stay as enjoyable as possible.</description>     <web>N/a</web>     <email>N/a</email>     <phone>N/a</phone>     <image></image> </pub> 

I am constantly getting xmlDoc = null. What is the problem here?

EDIT: In Chrome, under the network tab, it says that the xml document has been loaded. I am testing all this on the server my website is hosted, and not from my machine.

Source: http://stackoverflow.com/questions/12227776/loaded-xml-document-is-null-javascript

kevin durant jazz fest zurich classic selena lamichael james lamichael james derrick rose

No comments:

Post a Comment