PHP parse partial XML string

Hello friends,

I have a partial XML string that i would like to parse:

      <title>Some title</title>
      <description>Sample desc</description>
      <pubDate>Wed, 13 Mar 2019 10:06:13 +0100</pubDate>
      <media:content type="image/jpeg" url="http://www.url/">
        <media:credit scheme="urn:ebu">Timi</media:credit>
        <media:description>Image description</media:description>

I have tried to get the url attribute of media:content but failed, have tried with

    $xml = simplexml_load_string($xmlstr);
    $media = $xml->xpath("//media:content");
    var_dump((string) $media[0]->attributes()->url);


var_dump((string) $xml->media['url']);

But none seem to be able to get the attribute url of media:content.
Is this because its a partial XML? Because it gives some warnings on namespace errors.

Is there any other approach that could work, I do not need the have the XML validated.


1 Like

Thank you so much for your help! works like a charm!!

Is there any way to “search” for the media:content url?

I have another xml where the media:content is inside another tag

<media:content type="image/jpeg" url="http://www.url/">

A link for examples on simplexml :

this should work : $xml->{‘media:group’}->{‘media:content’}[‘url’]
Or you can use xpath (you have an example on the link i give)

For the benefit of other users, please translate your post otherwise I believe it will be removed.

Thank you

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.