Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    203

    Unanswered: How to referene perticuler xml data in php?

    Hi all. i have an xml file as shown below and i want to reference specific data out it.(The data shown in bold)Could any one show me how to reference those data so i can use them later as variables.I not only want to output them all at once.I also want to learn how to reference individual items separately because i have another part that uses those 3 variables(Artistname,songname,songimage).i be happy if some show me how this can be done in php.Thanks

    xml file(rss feed with one set of data always):
    Code:
    <playing>
    <artist>Cindy</artist> 
    <song>echo</song> 
    <image>http://www.somesite.com/song_images/cindy.jpg</image>
    <rating>3.5</rating>
    <songid>4736</songid>
    </playing>
    php code that uses retrevied data:

    Code:
    <?
    $imageurl = "http://www.somesite.com/song_images/cindy.jpg";
    $artist = "cindy";
    $song = "echo";
    .....
    .....
    
    ?>
    Last edited by mehran; 02-20-07 at 18:37.

  2. #2
    Join Date
    Mar 2007
    Posts
    24
    PHP contains a full library of xml functions. You can read up on them in the manual here.

    Go through the first coding example on that page-- that will give you a good idea of how PHP's xml parsing works.

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    If you are using PHP5 then read up on XPath and SimpleXML (which is a class). SimpleXML is a great utility for reading XML data easily.

    e.g.
    Code:
    $xmlstr = <<< END
    <root>
      <playing>
        <artist>Cindy</artist>
        <song>echo</song>
        <image>http://www.somesite.com/song_images/cindy.jpg</image>
        <rating>3.5</rating>
        <songid>4736</songid>
      </playing>
    </root>
    END;
    
    $obj_xml = new SimpleXMLElement($xmlstr);
    echo $obj_xml->playing[0]->image;
    echo $obj_xml->playing[0]->artist;
    echo $obj_xml->playing[0]->song;

    Edit : Before this will work you need to add a root node in. I have done this in the example above.
    Last edited by aschk; 04-03-07 at 08:48.

  4. #4
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Please note the example above is basing the fact that you wish to retrieve a single result from your set. What you could actually do is use a foreach loop.

    e.g.

    Code:
    foreach ($obj_xml->playing as $play) {
       echo $play->image, '<br />';
       echo $play->artist, '<br />';
       echo $play->song, '<br />';
    }
    This example will retrieve ALL the records and iterate through them outputting each image/artist/song.

  5. #5
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    please Note The Above Two Posts Are Based On Php5 Only

Posting Permissions

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