PHP parse partial XML string

Hello friends,

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

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

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);

and

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.

$xml->{ā€˜media:contentā€™}[ā€˜urlā€™]

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:group>
<media:content type="image/jpeg" url="http://www.url/">
</media:group>

A link for examples on simplexml :
http://php.net/manual/en/simplexml.examples-basic.php

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.

Edit:
Thank you

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