Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2012, 08:44 PM   PM User | #16
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 362 Times in 361 Posts
sunfighter is on a distinguished road
Quote:
How does the parser know which is meant to be a decimal, which a string, which a date, etc.
As has been said above, they are all text. If you can tell me how you can find the difference between them using the schema please let me know. I don't think the schema tells you if something is a time, a number, etc..

When I started to work on this I was under the impression that things would be simple in php because numbers and string normally change depending on whats being done to them and if you multiplied two variables they would try to cast them self as numbers. And javascript would be harder cause they don't. WRONG

I made a simple xml file, called multi.xml:
Code:
<?xml version="1.0"?>
<stuff>
  <area>
    <id>First</id>
    <base>100.25</base>
    <multiplyfactor>.5</multiplyfactor>
  </area>
  <area>
    <id>Second</id>
    <base>50</base>
    <multiplyfactor>0.5</multiplyfactor>
  </area>
</stuff>
Then worked on it in js and then in php. Here is the results and hope this will help you if you need to preform math on xml elements.
First js:
Code:
<!DOCTYPE html>
<html>
<body>
<script>
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","multi.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

var x=xmlDoc.getElementsByTagName("area");

for (i=0; i<=x.length; i++)
{
	var one = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
	var two = x[i].getElementsByTagName("base")[0].childNodes[0].nodeValue;
	var three = x[i].getElementsByTagName("multiplyfactor")[0].childNodes[0].nodeValue;

	document.write(one+"<br />");
	document.write(two+"<br />");
	document.write(three+"<br />");

	var four = three * two;
	document.write(two+" * "+three+" = "+four+"<br />---------<br />");
}
</script>
</body>
</html>
Here's the same thing in php. NOTE: I had a problem with the math because php did not change the variable to a number when I tried to multiply them together.
PHP Code:
<?php
$xml 
simplexml_load_file("multi.xml");
$area $xml->area;

for(
$i 0$i count($area); $i++)
{
    
$id $area[$i]->id;
    echo 
$id "<br />";
    
$base $area[$i]->base;
    echo 
$base "<br />";
    
$factor $area[$i]->multiplyfactor;
    echo 
$factor "<br />";
    if(!
is_numeric($base)) $base = (float)$base;
    if(!
is_numeric($factor)) $factor = (float)$factor;
    
$answer $base $factor;
    echo 
$answer;
    echo 
'<br />===================================<br />';
}
?>
The "How do you know which is a number" question. You have to know if your going to use it to preform math.
sunfighter is offline   Reply With Quote
Old 12-12-2012, 09:06 AM   PM User | #17
qwertyjjj
New Coder

 
Join Date: Dec 2007
Posts: 59
Thanks: 1
Thanked 0 Times in 0 Posts
qwertyjjj is an unknown quantity at this point
Quote:
Originally Posted by sunfighter View Post
As has been said above, they are all text. If you can tell me how you can find the difference between them using the schema please let me know. I don't think the schema tells you if something is a time, a number, etc..



The "How do you know which is a number" question. You have to know if your going to use it to preform math.
schemas should have a data type attribute - that's how you could tell the software what to look for.
<xs:element name="prizeamount" type="xs:decimal"/>
<xs:element name="prizecode" type="xs:integer"/>
qwertyjjj is offline   Reply With Quote
Old 12-13-2012, 02:51 PM   PM User | #18
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,498
Thanks: 18
Thanked 362 Times in 361 Posts
sunfighter is on a distinguished road
You know, for being the next BIG thing on the internet, there is so little information about it. I have found nothing about retrieving elements from the schema. Spent yesterday looking. From what I found out the schema is just used in writing the xml file and that's it. If you find out any thing let use know.
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 AM.


Advertisement
Log in to turn off these ads.