By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,201 Members | 1,617 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,201 IT Pros & Developers. It's quick & easy.

Need help with XML

P: 2

I am trying to make a xml-parser, but now I am stuck....

This is my xml:
[PHP]<Emission type="CO2">249</Emission>
<EUEconomy type="city">14.5</EUEconomy>
<EUEconomy type="road">8</EUEconomy>
<EUEconomy type="combined">10.4</EUEconomy>[/PHP]
..and now Im having problems with EUEconomies.. Cant get those values out of there, only first data and that one three times. What should I do so at I can get tose values out????
This is part of my PHP:
[PHP]if ($state=="EMISSION") { $userdata[$usercount]["Emission"] = $data;}
if ($state=="ACCELERATION") { $userdata[$usercount]["Acceleration"] = $data;}
if ($state=="MAXSPEED") { $userdata[$usercount]["MaxSpeed"] = $data;}
if ($state=="EUECONOMY") { $userdata[$usercount]["EUEconomy"] = $data;}
if ($state=="EUECONOMY") { $userdata[$usercount]["EUEconomy1"] = $data;}
if ($state=="EUECONOMY") { $userdata[$usercount]["EUEconomy2"] = $data;}[/PHP]and[PHP]if (!($xml_parser = xml_parser_create())) die("Couldn't create parser.");
xml_set_element_handler( $xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler( $xml_parser, "characterDataHandler");

//while( $data = fread($fp, 16384)){
while( $data = fread($fp, 8192)){
if(!xml_parse($xml_parser, $data, feof($fp))) {
for ($i=0;$i<$usercount; $i++)[/PHP]
and[PHP]$Emission = $userdata[$i]["Emission"];
$Acceleration = $userdata[$i]["Acceleration"];
$MaxSpeed = $userdata[$i]["MaxSpeed"];
$EUEconomy1 = $userdata[$i]["EUEconomy1"];[/PHP]

Thanks for advance
Mar 4 '08 #1
Share this Question
Share on Google+
1 Reply

Expert 5K+
P: 5,821
Heya, Commander.

In this block:
Expand|Select|Wrap|Line Numbers
  1. if ($state=="EUECONOMY") { $userdata[$usercount]["EUEconomy"] = $data;}
  2. if ($state=="EUECONOMY") { $userdata[$usercount]["EUEconomy1"] = $data;}
  3. if ($state=="EUECONOMY") { $userdata[$usercount]["EUEconomy2"] = $data;}
Since you're using if() instead of if()...elseif(), PHP executes all three statements when $state is 'EUECONOMY'.

Also, note that you have three identical conditionals, which is probably not what you want.

You might try something like this:
Expand|Select|Wrap|Line Numbers
  1. elseif( $state == 'EUECONOMY' )
  2. {
  3.   $userdata[$usercount]['EUEconomy'][] = $data;
  4. }
Note the '[]' at the end.
Jun 27 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.