Jump to content

need help reading XML


attaboy

Recommended Posts

PHP 5.3.8

XAMPP on Windows XP

 

I have an XML file that manages the input to an mp3 player on this page http://www.jimslounge.com/segovia/

The structure looks like this

<content>

  <auto_play>yes</auto_play>
  <loop>yes</loop>
  <volume>60</volume>

  <artist>
    <song_title><![CDATA[bach - Suite No. 3 - Allemande]]></song_title>
    <artist_name><![CDATA[Andre Segovia]]></artist_name>
  	<image_path>load/images/segovia.jpg</image_path>
	<mp3_path>load/songs/Bach - Suite No. 3 (for solo cello, arr. Duarte)- Allemande.mp3</mp3_path>
<url target="_blank"  open="yes">http://en.wikipedia.org/wiki/Andr%C3%A9s_Segovia</url>
  </artist>
  <artist>
      ...

 

with <artist> being the repeating element I want to be able to read and eventually write to

If I run this

 $xml = simplexml_load_file("load.xml");
var_dump($xml);

 

I get this

object(SimpleXMLElement)#1 (4) { ["auto_play"]=> string(3) "yes" ["loop"]=> string(3) "yes" ["volume"]=> string(2) "60" ["artist"]=> array(22) { [0]=> object(SimpleXMLElement)#2 (5) { ["song_title"]=> object(SimpleXMLElement)#24 (0) { } ["artist_name"]=> object(SimpleXMLElement)#25 (0) { } ["image_path"]=> string(23) "load/images/segovia.jpg" ["mp3_path"]=> string(74) "load/songs/Bach - Suite No. 3 (for solo cello, arr. Duarte)- Allemande.mp3"  ...

which is overwhelming

 

This cleans it up but doesn't give me the artist name and song name I'm looking for.

 

$xml = simplexml_load_file("load.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }

 

The Output:

content

auto_play: yes

loop: yes

volume: 60

artist:

artist:

artist:

 

My experience with simplexml only goes back a couple of hours so I'm pretty inept at this point.  Any help will be greatly appreciated.

 

Link to comment
Share on other sites

When you loop through $xml->children(), you will get an object for each child. So you would have a separate object for auto_play, loop, volume, and artist. These elements then become the root. So to get the song titles, you would do:

 

foreach($xml->children() as $child)
{
echo $child->song_title[0] . '<br />';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.