Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2016
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Reading Values from XML and using results with Promise

    I am reading a local XML file and outputing the results as variables and i want to check that these are "available" else produce the error. Currently this code is saying that z equals empty, but it is not. Basically I want this to check each one in turn (check z, c, f in this order), and if any of them are empty produce the error message.

    Code:
    <script>
    		var promise = new Promise(function(resolve, reject){
    			var xmlhttp = new XMLHttpRequest();
    			  xmlhttp.onreadystatechange = function() {
    			    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    			      myFunction(xmlhttp);
    			    }
    			  };
    			  xmlhttp.open("GET", "cd_catalog.xml", true);
    			  xmlhttp.send();
    			
    			function myFunction(xml) {
    			  var xmlDoc = xml.responseXML;
    			  x = xmlDoc.getElementsByTagName("IP")[0];
    			  y = x.childNodes[0];
    			  z = y.nodeValue;
    			  console.log(z);
    			  a = xmlDoc.getElementsByTagName("CountryName")[0];
    			  b = a.childNodes[0];
    			  c = b.nodeValue;
    			  console.log(c);
    			  d = xmlDoc.getElementsByTagName("City")[0];
    			  e = d.childNodes[0];
    			  f = e.nodeValue;
    			  console.log(f);
    			}
    			  if(myFunction(z == "")){
    				resolve("Stuff worked");
    			}	
    			else{
    				reject(new Error("It broke"));
    			}
    		});	
    		promise
    				.then(result => {
    					console.log(y + " " + "This is awesome" + " " + y * 5);
    				})
    			  .catch(error => {
    			  	fallbackimg = new Image();
    			  	fallbackimg.src = "http://3.bp.blogspot.com/-fxkwVPW57DY/UBfvtElw15I/AAAAAAAAAMc/xwlCZJEZ2Lg/s1600/love-color-rain-Love-hearts-ngi-5-500x500-Bookmarks-406647-normal_large.png";
    			  	fallbackimg.alt ="";
    			  	document.getElementById("demo").appendChild(fallbackimg);
    			  });
    			  </script>
    I am relatively new to promises so direction is really appreciated.

    Thanks
    Rustic

  2. #2
    New to the CF scene
    Join Date
    May 2016
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry please delete. Ive realised ive posted this in the wrong forum XD


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •