Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2007
    Posts
    162
    Thanks
    13
    Thanked 0 Times in 0 Posts

    trouble parsing xml using SimpleXMLElement

    Here's my script:

    PHP Code:
    <?php

    ini_set
    ('display_errors'1);

    error_reporting(E_ALL|E_STRICT);

    // script: csv_to_xml.php 
    // description: retrieves csv file of recreation site data and converts to xml
    // altered to grab snow depth from tripcheck xml data
    // author: Shad Keene
    // date: 3/2/2012


    $file "http://www.greenandtheblue.com/weather/roadtest.xml";


    $data = @file_get_contents($file);
    $xml_file_data = new SimpleXMLElement($data);
    echo 
    '<pre>';
    print_r($xml_file_data);
    echo 
    '</pre>';

        

    $i 0;

    while (
    $xml_file_data->weatherReports->weatherReport[$i]->location->locationName != ''){
      
    if (
    preg_match("/LK OF THE WOODS/"$xml_file_data->weatherReports->weatherReport[$i]->location->locationName)) {
    echo
    "found lake of the woods";
    }

    $i++;


    }



    ?>
    I'm trying to get locationname tags to key in on certain locations, but it's giving errors. I've tried some different things, but nothing has worked. I'm just trying to get wired in to the way this xml is organized and how to access the various elements. I've taken a look at a few tutorials, but they seemed too basic.

    Thanks for any help!

    S

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Here is how you could do it with xPath:
    PHP Code:
    <?php 

    $file 
    'http://www.greenandtheblue.com/weather/roadtest.xml'
    $data file_get_contents($file); 
    $xml_file_data = new SimpleXMLElement($data);

    echo 
    '<pre>'

    $result $xml_file_data->xpath('/informationResponse/responseGroups/responseGroup/weatherReports/weatherReport/location[locationName = "LK OF THE WOODS"]/../*');

    echo 
    'Location: ' . (string) $result[1]->locationName "\n";
    echo 
    'currTemp: ' . (string) $result[2] . "\n";
    echo 
    'highTemp: ' . (string) $result[3] . "\n";
    echo 
    'lowTemp: ' . (string) $result[4] . "\n";

    echo 
    'snowDepth: ' . (string) $result[6] . "\n";
    echo 
    'snowOffRoad: ' . (string) $result[7] . "\n";
    echo 
    'drivingRestrictions: ' trim((string) $result[8]) . "\n";

    echo 
    '</pre>';
    Outputs:
    Code:
    Location: LK OF THE WOODS
    currTemp: -6.1
    highTemp: 1001
    lowTemp: 1001
    snowDepth: 3001
    snowOffRoad: 96.52
    drivingRestrictions: Carry chains or traction tires.

  • #3
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,229
    Thanks
    10
    Thanked 270 Times in 269 Posts
    This might be useful in terms of easier to read:
    PHP Code:
    $result $xml_file_data->xpath('/informationResponse/responseGroups/responseGroup/weatherReports/weatherReport/location[locationName = "LK OF THE WOODS"]/..');
    echo 
    'Location: ' .$result[0]->location->locationName "\n";
    echo 
    'currTemp: ' .$result[0]->currTemp"\n";
    echo 
    'highTemp: ' .$result[0]->highTemp "\n";
    echo 
    'lowTemp: ' .$result[0]->lowTemp"\n";
    echo 
    'drivingRestrictions: ' trim($result[0]->drivingRestrictions) . "\n";
    echo 
    '</pre>'
    Dave


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •