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

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2

    Parsing XML NULL


    I'm trying to parse xml I loaded onto the page by simplexml_load_file and I'm using the following code to try to do this,


    PHP Code:
    $resp simplexml_load_file($request_url);
    $shipping $resp->rate[15]['total_charge'];
    var_dump($shipping); 
    However I get NULL. Here is what the xml looks like when var dumped from $resp.

    PHP Code:
    object(SimpleXMLElement)#1 (1) { ["rate"]=> object(SimpleXMLElement)#2 (1) { ["@attributes"]=> array(15) { ["weight"]=> string(3) "5.0" ["weight_unit"]=> string(2) "lb" ["zone"]=> string(1) "3" ["transit_time"]=> string(1) "1" ["transit_time_guaranteed"]=> string(5) "false" ["estimated_delivery_date"]=> string(10) "2013-07-31" ["base_charge"]=> string(5) "11.05" ["freight_charge"]=> string(5) "11.05" ["residential_address_charge"]=> string(4) "2.75" ["net_charge"]=> string(5) "13.80" ["fuel_surcharge_rate"]=> string(4) "17.3" ["fuel_surcharge"]=> string(4) "1.91" ["subtotal_charge"]=> string(5) "15.71" ["on_hst_charge"]=> string(4) "2.04" ["total_charge"]=> string(5) "17.75" } } } 
    and I'm trying to put this guy, ["total_charge"]=>string(5) "17.75" into it's own variable in php called $shipping so I can echo it out as $17.75.

    Thanks for your time.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,571
    Rep Power
    595
    Clearly your reference must be wrong. However it is too hard (for me) to read as a single line. Use this to dump the variable in a more readable form.
    PHP Code:
    echo "<pre>";
    var_dump($resp);
    echo 
    "</pre>"
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    Hey, I tried taking out [15] and got this error.

    object(SimpleXMLElement)#4 (1) { [0]=> string(5) "17.75" }
    Warning: number_format() expects parameter 1 to be double, object given in
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    120
    Rep Power
    2
    nvm I put (float) infront and that worked! thanks for the help!
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,245
    Rep Power
    9400
    Pretty much everything coming from a SimpleXMLElement object is itself a SimpleXMLElement. Even attributes.

    As you've discovered, typecasting is your friend.

IMN logo majestic logo threadwatch logo seochat tools logo