Need help with XML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • commander
    New Member
    • Mar 2008
    • 2

    #1

    Need help with XML

    Hello

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

    This is my xml:
    [PHP]<Emission type="CO2">249</Emission>
    <Acceleration>7 .6</Acceleration>
    <MaxSpeed>250 </MaxSpeed>
    <EUEconomy type="city">14. 5</EUEconomy>
    <EUEconomy type="road">8</EUEconomy>
    <EUEconomy type="combined" >10.4</EUEconomy>[/PHP]
    ..and now I´m 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=="EMISS ION") { $userdata[$usercount]["Emission"] = $data;}
    if ($state=="ACCEL ERATION") { $userdata[$usercount]["Accelerati on"] = $data;}
    if ($state=="MAXSP EED") { $userdata[$usercount]["MaxSpeed"] = $data;}
    if ($state=="EUECO NOMY") { $userdata[$usercount]["EUEconomy"] = $data;}
    if ($state=="EUECO NOMY") { $userdata[$usercount]["EUEconomy1 "] = $data;}
    if ($state=="EUECO NOMY") { $userdata[$usercount]["EUEconomy2 "] = $data;}[/PHP]and[PHP]if (!($xml_parser = xml_parser_crea te())) die("Couldn't create parser.");
    xml_set_element _handler( $xml_parser, "startElementHa ndler", "endElementHand ler");
    xml_set_charact er_data_handler ( $xml_parser, "characterDataH andler");

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

    Thanks for advance
    Last edited by ronverdonk; Mar 4 '08, 11:40 AM. Reason: arrange code
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Commander.

    In this block:
    [code=php]
    if ($state=="EUECO NOMY") { $userdata[$usercount]["EUEconomy"] = $data;}
    if ($state=="EUECO NOMY") { $userdata[$usercount]["EUEconomy1 "] = $data;}
    if ($state=="EUECO NOMY") { $userdata[$usercount]["EUEconomy2 "] = $data;}
    [/code]

    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:
    [code=php]
    elseif( $state == 'EUECONOMY' )
    {
    $userdata[$usercount]['EUEconomy'][] = $data;
    }
    [/code]

    Note the '[]' at the end.

    Comment

    Working...