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