Jump to content

[SOLVED] SimpleXML PHP5.2 problem


myselfandi

Recommended Posts

Hello guys,

 

It's cool that there's people out there willing to help others. Thank you.

 

I'm having trouble getting the right values out of this weirdly formatted XML code:

<Schedule System="Jazler">
<Event status="happening" startTime="21:38:56" eventType="song">
    <Announcement Display="Now On Air:"/>
  <Song title="I'll worship only">
   <Album title="-"/>
   <Artist name="The Cathedral">
    <Media runTime="258.205"/>
    <Expire Time="21:43:14"/>
    <TrackID AUID="1073"/>
   </Artist>
  </Song>
</Event>
</Schedule>

 

What I am trying to do is extract the song title and the artist's name. I've been using all kinds of code snippets I found all over the place, but none seem to work unless the XML is formatted with the values in between <tag>value</tag>. And I can't re-format that XML because it's being generated by some weird radio software called Jazler.

 

So here's what I have so far:

<?php
$xml = simplexml_load_file("try.xml");

//echo $xml->getName() . "<br />";
/*

foreach($xml->children() as $child)
  {
  echo $child->getName() . ": " . $child . "<br />";
  }
  
  echo "<br>trying print_r($xmlobj);<br>"
*/
// print_r($xml);

  //print $xmlobj->Schedule;
  //print $xmlobj->name;
$stuff = $xml->xpath("/Schedule/Event/Song/Artist") ;
//print_r($stuff);
  echo "<br>trying to extract the right thing<br>";
//extract($stuff,EXTR_PREFIX_SAME,d);
//  echo "<br>stuff->name is " . $stuff->Media;
//$test = implode("=",$stuff);
$test = join($stuff);
echo $test;
?> 

 

I'm obviously getting nowhere, and as you can see I tried several options on there (commented out).

So the problem is, the array that gets created once simpleXML comes in is totally whacked out. If I do a print_r($xml); it doesn't look like an array to me.

 

What I'm thinking is that since the XML is not formatted the right way, maybe simpleXML is having trouble building said array and hence I cannot use the nifty little php5 specific trick. That being said, how do I just combine that array into a string that I can further play with using regex to only extract what I need?

 

When using implode(); on $xml, I get this error:

Warning: implode() [function.implode]: Invalid arguments passed in /path/to/try.php on line 23

If however, I try using join(); it gives me some more detail about it. It appears $xml is not an array. I'm lost here, I don't get it. Help? :)

Link to comment
https://forums.phpfreaks.com/topic/164979-solved-simplexml-php52-problem/
Share on other sites

<?php
$xml = simplexml_load_file("try.xml");
print $xml->Event->Song['title'];
?>

 

Oh my! It took me 3 hours to get something working, and you nailed it in 1 line of code! I can't even describe how puny I feel. Thank you much.

 

Here's what I got to after 3 hours of reading and googling...:

$do = $xml->xpath("//@name");

Which I must say is like walking blindly through the array and then hoping you get the right title from the array :) - attribute title is doubled up for the Song and Album.

 

Again, thank you much.

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.