Jump to content

simple xml


dreamwest

Recommended Posts

 

Im trying to parse this xml but i can get it to output a simple string, can someone see what im doing wrong??

 

$str = "<channel><item>
		<id>cb-uA4QHmpDxYdpgW1jPwiYsUwjBViwlinS</id>
		<title>Rules of Engagement - Play Ball Extended Preview - Season 5 - Episode 12</title>
</item>
		</channel>";


$n_data = new SimpleXmlElement($str, LIBXML_NOCDATA);
		foreach ($n_data->channel->item as $d) {
		$vid = $d->id;
		$title = $d->title;

		 print_r($d->title); //testing
		 die(); //testing
		 $arr2[] = array('vid' => $vid, 'title' => $title, 'site_id' => 10 ,'time' => 456);
		}

 

The print_r() returns

SimpleXMLElement Object
(
    [0] => Rules of Engagement - Play Ball Extended Preview - Season 5 - Episode 12
)

 

If i simply echo $d->title it returns a string, but i need to reapply $d->title to another array as shown above

Link to comment
https://forums.phpfreaks.com/topic/222196-simple-xml/
Share on other sites

Actually i thought that worked but the problem is $title in the new array get the

 

print_r($title)

	SimpleXMLElement Object
(
    [0] => Rules of Engagement - Play Ball Extended Preview - Season 5 - Episode 12
)}

 

print_r($arr2);

Array
(
    [0] => Array
        (
            [vid] => SimpleXMLElement Object
                (
                    [0] => cb-uA4QHmpDxYdpgW1jPwiYsUwjBViwlinS
                )

            [title] => SimpleXMLElement Object
                (
                    [0] => Rules of Engagement - Play Ball Extended Preview - Season 5 - Episode 12
                )

            [site_id] => 10
            [time] => 456
        )

    [1] => Array
        (
            [vid] => SimpleXMLElement Object
                (
                    [0] => cb-pOEUN4deIJ2cc_22ZjFCaCIcgimN6D2A
                )

            [title] => SimpleXMLElement Object
                (
                    [0] => David Letterman - Matt Damon: Tongue-Tied - Season 18 - Episode 3415
                )

            [site_id] => 10
            [time] => 456
        )
)

 

 

Link to comment
https://forums.phpfreaks.com/topic/222196-simple-xml/#findComment-1149470
Share on other sites

Your main problem is using $n_data->channel->item where you want to be using $n_data->item. When you construct a new SimpleXMLElement instance like you're doing, the document's root node (<channel>) is what that instance represents. You'll also probably want to cast the values to strings when you put them into the $arr2 array.

Link to comment
https://forums.phpfreaks.com/topic/222196-simple-xml/#findComment-1149493
Share on other sites

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.