Home > Enterprise >  PHP parsing xml with node children
PHP parsing xml with node children

Time:06-04

The RSS feed https://moxie.foxnews.com/feedburner/world.xml contains items like:

<link></link>
<title></title>
<media:group>
  <media:content url="*.jpg" medium="image" isDefault="true"/>
  <media:content url="*.jpg" medium="image" width="60" height="60"/>
</media:group>
<media:thumbnail url="*.jpg" width="60" height="60"/>

I know how to display the contents of <link> and <title> but can anyone help me display the media URLs inside <media:group><media:content> and <media:thumbnail>? Thanks

CodePudding user response:

I've cleaned up the badly formed XML so it parses and provided a way to see what the errors are.

$xml = '<media:group>
  <link></link>
  <title></title>
  <media:content url="*.jpg" medium="image" isDefault="true"></media:content>
  <media:content url="*.jpg" medium="image" width="60" height="60"></media:content>
</media:group>';

libxml_use_internal_errors(true);
$doc = simplexml_load_string($xml);
if ($doc === false) {
  $errors = libxml_get_errors();
  print_r($errors);
} else {
  print_r($doc);
}

result

SimpleXMLElement Object
(
    [link] => SimpleXMLElement Object
        (
        )

    [title] => SimpleXMLElement Object
        (
        )

    [media:content] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [url] => *.jpg
                            [medium] => image
                            [isDefault] => true
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [url] => *.jpg
                            [medium] => image
                            [width] => 60
                            [height] => 60
                        )

                )

        )

)

Or direct from the URL

libxml_use_internal_errors(true);
$doc = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
if ($doc === false) {
  $errors = libxml_get_errors();
  print_r($errors);
} else {
  print_r($doc);
}

// Result

SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 )

[channel] => SimpleXMLElement Object
    (
        [title] => FOX News : World  
        [description] => SimpleXMLElement Object
            (
            )

        [image] => SimpleXMLElement Object
            (
                [url] => https://global.fncstatic.com/static/orion/styles/img/fox-news/logos/fox-news-desktop.png
                [title] => FOX News : World  
                [link] => https://www.foxnews.com
            )

        [link] => https://www.foxnews.com
        [item] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [guid] => https://www.foxnews.com/world/us-taiwan-launch-trade-talks-biden-excludes-island-indo-pacific-group
                        [link] => https://www.foxnews.com/world/us-taiwan-launch-trade-talks-biden-excludes-island-indo-pacific-group
                        [category] => Array
                            (
                                [0] => c8e27233-08e3-5b74-9eff-da5e9fe271ff
                                [1] => fox-news/world/world-regions/asia
                                [2] => fox-news/world/world-regions/china
                                [3] => fox-news/person/joe-biden
                                [4] => fox-news/politics/foreign-policy
                                [5] => fnc
                                [6] => fox-news/world
                                [7] => article
                                [8] => Reuters
                            )

                        [title] => US, Taiwan to launch trade talks after Biden excludes island from Indo-Pacific group
                        [description] => SimpleXMLElement Object
                            (
                            )

                        [pubDate] => Wed, 01 Jun 2022 15:25:51 GMT

Thanks for the clarification.

$xml = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
foreach ($xml->channel->item as $item) {
  foreach ($item->children('media', true) as $mg) {
    $media_group = $mg->content->attributes()['url'];
    print_r($media_group);
  }
}

// Result

SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/lech-walesa.jpg
)
SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/taiwan-us-flag.jpg
)
SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/05/Putin.jpg
)
SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/05/GettyImages-1395351622-1-e1651678769232.jpg
)
SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/04/Dmitry-Peskov.png
)
SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/06/Ukraine-President-Volodymyr-Zelenskyy-Russia-War.jpg
)
SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/04/Israeli-Soldier.jpg
)
SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2021/11/chinaforeignministryspokesman.jpg
)
SimpleXMLElement Object
(
    [0] => https://static.foxnews.com/foxnews.com/content/uploads/2022/02/zelenskyy-biden-split-2-25-22.jpg
)

Update

$rss = simplexml_load_file('https://moxie.foxnews.com/feedburner/world.xml');
foreach ($rss->channel->item as $item) {
  echo '<p><a href="' . $item->link . '">' . $item->title . "</a></p>";
  echo "<p>" . $item->description . "</p>";
  foreach ($item->children('media', true) as $mg) {
    foreach ($mg->children('media', true) as $c) {
      echo '<img src="' . $c->attributes()['url'] . '"><br>';
    }
  }
}

// Output (note second image is a thumbnail)

<p>
  <a href="https://www.foxnews.com/world/russia-global-food-crisis-49-million-famine-starvation-expert">Russia-induced global food crisis pushes 49M to 'brink' of famine, starvation, expert warns</a>
</p>
<p>Russia's war in Ukraine has sparked a global food crisis with 49 million facing famine. Now, experts are worried the world faces an increased security threat.</p>
<img src="https://static.foxnews.com/foxnews.com/content/uploads/2021/09/World-Food-Program-Kabul-Afghanistan..jpg"><br>
<img src="https://a57.foxnews.com/static.foxnews.com/foxnews.com/content/uploads/2021/09/60/60/World-Food-Program-Kabul-Afghanistan..jpg?ve=1&tl=1">
  • Related