#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0

    NodeValue problem


    Hi guys I'm new to xml and I'm struggling on this one. Below are my html and xml file.

    HTML CODE
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    
        <title>Checking temperatures</title>
    
        <script language = "javascript">
          function getDoc()
          {	 
    
          if (window.XMLHttpRequest) {
              request = new XMLHttpRequest();
           } 
    	   else if (window.ActiveXObject) {
              request = new ActiveXObject("Microsoft.XMLHTTP");
            }
    	    if (request.overrideMimeType) {         
    		  request.overrideMimeType("text/xml");
    		}
    
            if(request) {
              request.open("GET", "temperatures.xml", true); 
    
              request.onreadystatechange = function() 
    		  
              { 
                if ((request.readyState == 4) && (request.status == 200)) { 
                var xmlDocument = request.responseXML;
    			findClass(xmlDocument);
                }
               
              } 
    		  request.send(null); 
              } 
    		  
          }
    	  
    	  function findClass(doc) {
    	  
    	  var sum = 0;	
    	  var temp = doc.getElementsByTagName('temperature');
    	  for (i=0; i<temp.length; i++) {
    	    var day = temp[i].getElementsByTagName('day');
    	    var dayValue = day[0].firstChild.nodeValue;
    	    var month = temp[i].getElementsByTagName('month');
    	    var monthValue = month[0].firstChild.nodeValue;
    	    var year = temp[i].getElementsByTagName('year');
    	    var yearValue = year[0].firstChild.nodeValue;
               var max = temp[i].getElementsByTagName('max_temperature');
    	    var maxValue = max[0].firstChild.nodeValue;
    	    	
    	    sum = sum + maxValue;	
    	    var displaytemp = dayValue + "/" + monthValue + "/" + yearValue + " : " + maxValue + " " + sum;
    	  
    	    var myEl = document.createElement('p');
            var myTx = document.createTextNode(displaytemp);
    	
            myEl.appendChild(myTx);
    	
            var temperature = document.getElementById('title1');
    	
            temperature.appendChild(myEl);
    	} 
    }
     
    </script>
    
    </head>
    
     <body>
    
        <h1>Checking temperatures</h1>
        <form>
          <input type = "button" id="reqDoc" value = "Check temperatures"> 
        </form>
    <script type="text/javascript">
    var myDoc = document.getElementById('reqDoc');
    myDoc.onclick = getDoc;
    </script>
    <div id="title1"></div>
    </body>
    </html>
    XML FILE
    Code:
    <?xml version="1.0"?>
    <temperatures>
    	<temperature>
    		<day>1</day>
    		<month>4</month>
    		<year>2010</year>
    		<max_temperature>24.0</max_temperature>
    	</temperature>
    	<temperature>
    		<day>2</day>
    		<month>4</month>
    		<year>2010</year>
    		<max_temperature>21.9</max_temperature>
    	</temperature>
    	<temperature>
    		<day>3</day>
    		<month>4</month>
    		<year>2010</year>
    		<max_temperature>24.4</max_temperature>
    	</temperature>
    	<temperature>
    		<day>4</day>
    		<month>4</month>
    		<year>2010</year>
    		<max_temperature>25.8</max_temperature>
    	</temperature>
    	<temperature>
    		<day>5</day>
    		<month>4</month>
    		<year>2010</year>
    		<max_temperature>26.9</max_temperature>
    	</temperature>
    	<temperature>
    		<day>6</day>
    		<month>4</month>
    		<year>2010</year>
    		<max_temperature>25.3</max_temperature>
    	</temperature>
    	<temperature>
    		<day>7</day>
    		<month>4</month>
    		<year>2010</year>
    		<max_temperature>25.7</max_temperature>
    	</temperature>
    </temperatures>
    In my html file, when I try to add maxValue to sum it doesn't work.

    example:
    sum = 10;
    maxValue = 24.0;

    sum = sum + maxValue;

    Output is 1024.0 instead of 34.0

    My question is how can I get the maxValue to be parse as an integer so as I can add it to sum. I only want to use DOM to achieve that and not using XSLT.

    If anyone can help. thanks in advance.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    24
    Rep Power
    0
    I find a solution. I use parseFloat to convert maxValue into integer.

IMN logo majestic logo threadwatch logo seochat tools logo