Results 1 to 3 of 3

Thread: How to replace an xml element name

  1. #1
    Join Date
    Mar 2009
    Location
    San Diego, CA
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to replace an xml element name

    I am pulling in xml data from the National Weather Service to my php5 file.

    I can pull the temperature data: $xml->data->parameters->temperature[0]->value;

    However, I can not pull in the wave height data because the element id name includes a dash:
    $xml->data->parameters->water-state[0]->waves[0]->value;

    It appears to me I should use str_replace() to replace water-state with water_state. Unfortunately, I've been unsuccessful in my attempts. Not sure if I'm applying it incorrectly or if I need to use a different approach.


    Here is the code I'm using. Please help me apply the str_replace() function correctly or suggest a different approach. Thank you.

    Code:
     <?php
    $xml = simplexml_load_file("http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLclient.php?whichClient=NDFDgen&lat=32.84&lon=-117.26&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&listLat1=&listLon1=&listLat2=&listLon2=&resolutionList=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&listEndPoint1Lat=&listEndPoint1Lon=&listEndPoint2Lat=&listEndPoint2Lon=&zipCodeList=&listZipCodeList=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=&sector=&gmlListLatLon=&featureType=&requestedTime=&startTime=&endTime=&compType=&propertyName=&product=time-series&begin=2004-01-01T00%3A00%3A00&end=2013-03-03T00%3A00%3A00&maxt=maxt&waveh=waveh&Submit=Submit");
    ?>
    <?php echo $xml->data->parameters->temperature[0]->value; ?> &deg;<br />
    <?php echo $xml->data->parameters->water_state[0]->waves[0]->value; ?> Feet

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    I have never needed to do something like this before, but this is what I came up this:
    PHP Code:
    <?php
    $xml 
    simplexml_load_file("http://www.weather.gov/forecasts/xml/SOAP_server/ndfdXMLclient.php?whichClient=NDFDgen&lat=32.84&lon=-117.26&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&listLat1=&listLon1=&listLat2=&listLon2=&resolutionList=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&listEndPoint1Lat=&listEndPoint1Lon=&listEndPoint2Lat=&listEndPoint2Lon=&zipCodeList=&listZipCodeList=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon=&resolutionSquare=&listCenterPointLat=&listCenterPointLon=&listDistanceLat=&listDistanceLon=&listResolutionSquare=&citiesLevel=&listCitiesLevel=&sector=&gmlListLatLon=&featureType=&requestedTime=&startTime=&endTime=&compType=&propertyName=&product=time-series&begin=2004-01-01T00%3A00%3A00&end=2013-03-03T00%3A00%3A00&maxt=maxt&waveh=waveh&Submit=Submit");
    ?>
    <?php 
    echo $xml->data->parameters->temperature[0]->value?> &deg;<br />
    <?php echo $xml->data->parameters->{'water-state'}[0]->waves[0]->value?> Feet
    (If someone has a better idea/way, I would love to see what it is)

  3. The Following User Says Thank You to techietim For This Useful Post:

    wilm1998 (03-09-2009)

  4. #3
    Join Date
    Mar 2009
    Location
    San Diego, CA
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much! I've got it up and running: http://thebestplacesinsandiego.com/

Bookmarks

Posting Permissions

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