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
https://forums.phpfreaks.com/topic/262651-need-help-reading-xml/
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 />';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.