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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post

    Accessing XML element

    Hello, Yahoo Weather has following XML structure (showing part of it):

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
    <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
    <channel>
      <title>Yahoo! Weather - Sunnyvale, CA</title>
      <link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
      <description>Yahoo! Weather for Sunnyvale, CA</description>
      <language>en-us</language>
      <lastBuildDate>Fri, 18 Dec 2009 9:38 am PST</lastBuildDate>
      <ttl>60</ttl>
      <yweather:location city="Sunnyvale" region="CA"   country="United States"/>
      <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
      <yweather:wind chill="50"   direction="0"   speed="0" />
      <yweather:atmosphere humidity="94"  visibility="3"  pressure="30.27"  rising="1" />
      <yweather:astronomy sunrise="7:17 am"   sunset="4:52 pm"/>
      <image>
        <title>Yahoo! Weather</title>
        <width>142</width>
        <height>18</height>
        <link>http://weather.yahoo.com</link>
        <url>http://l.yimg.com/a/i/us/nws/th/main_142b.gif</url>
      </image>
      <item>
        <title>Conditions for Sunnyvale, CA at 9:38 am PST</title>
        <geo:lat>37.37</geo:lat>
        <geo:long>-122.04</geo:long>
        <link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
        <pubDate>Fri, 18 Dec 2009 9:38 am PST</pubDate>
        <yweather:condition  text="Mostly Cloudy"  code="28"  temp="50"  date="Fri, 18 Dec 2009 9:38 am PST" />
        <description><![CDATA[
    <img src="http://l.yimg.com/a/i/us/we/52/28.gif"/><br />
    <b>Current Conditions:</b><br />
    Mostly Cloudy, 50 F<BR />
    <BR /><b>Forecast:</b><BR />
    Fri - Partly Cloudy. High: 62 Low: 49<br />
    Sat - Partly Cloudy. High: 65 Low: 49<br />
    <br />
    <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
    (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
    ]]></description>
        <yweather:forecast day="Fri" date="18 Dec 2009" low="49" high="62" text="Partly Cloudy" code="30" />
        <yweather:forecast day="Sat" date="19 Dec 2009" low="49" high="65" text="Partly Cloudy" code="30" />
        <guid isPermaLink="false">USCA1116_2009_12_18_9_38_PST</guid>
      </item>
    </channel>
    </rss>
    Accessing regular xml elements with PHP is no problem:

    PHP Code:
    $title $xml->channel->item->title;
    $description  $xml->channel->item->description

    However, I get parse errors when accessing elements in form of <yweather:location>...

    PHP Code:
    $condition $xml->channel->item->yweather:condition[text]; 
    gives me a parse error...


    What is the correct PHP syntax to access the attributes?

    Thanks for your help,

    guvenck

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Hi guvenck,

    Good day!

    The actual tag name is location. The "yweather:" is the namespace, so try accessing them without it.

    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,436
    Thanks
    13
    Thanked 360 Times in 356 Posts
    not sure this will work. you have to access the elements with their namespace (ref.). more on google.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    377
    Thanks
    8
    Thanked 1 Time in 1 Post
    @rangana,

    Thank you for your answer,

    I tried these and both of them did not work:

    PHP Code:

    // this throws an empty value
    $condition $xml->channel->item->condition[text];



    // this throws an error:
    // Warning: main() [function.main]: Node no longer exists in weather.php on line 14

    $condition $xml->channel->item->condition->attributes()->text
    Any more ideas?

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,436
    Thanks
    13
    Thanked 360 Times in 356 Posts
    Quote Originally Posted by guvenck View Post
    Any more ideas?
    read the article in the given link, that should explain it.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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