Using SimpleXml I read a piece of tape

<item> <title>The Justices Lay Down the Law</title><pubDate>Wed, 28 Jun 2017 14:07:05 -0500</pubDate> <fullpubdate>06/28/2017/00/00/00</fullpubdate> <description><![CDATA[ <a href="/authors/david_rivkin" data-mce-href="../../authors/david_rivkin">Rivkin</a> &amp; <a href="/authors/lee_casey" data-mce-href="../../authors/lee_casey">Casey</a>, <span class="source">Wall Street Journal</span><br />In the travel-ban case, a high-court &lsquo;compromise&rsquo; delivers a unanimous rebuke to political judges.]]></description><link>https://www.realclearpolitics.com/2017/06/28/the_justices_lay_down_the_law_414173.html</link><originalLink>https://t.co/y0BkZIKyc1</originalLink> <guid isPermaLink="false">100414173</guid> <category>PM Update</category> <author><![CDATA[ <a href="/authors/david_rivkin" data-mce-href="../../authors/david_rivkin">Rivkin</a> &amp; <a href="/authors/lee_casey" data-mce-href="../../authors/lee_casey">Casey</a>, Wall Street Journal]]></author><media:content url="https://assets.realclear.com/images/41/419326_1_.jpg" type="image/jpeg" height="190" width="250" /> <media:thumbnail url="https://assets.realclear.com/images/41/419326_3_.jpg" height="60" width="90" /> <media:title>The Justices Lay Down the Law</media:title> </item> $content_data = @file_get_contents($this->data_source); $xmlDataArray = new \SimpleXmlElement($content_data); foreach($xmlDataArray->channel->item as $nextXmlRow) { dump($nextXmlRow); dump($nextXmlRow->media); dump($nextXmlRow->author); 

And I get the output:

 SimpleXMLElement {#368 ▼ +"title": "The Justices Lay Down the Law" +"pubDate": "Wed, 28 Jun 2017 14:07:05 -0500" +"fullpubdate": "06/28/2017/00/00/00" +"description": SimpleXMLElement {#373} +"link": "https://www.realclearpolitics.com/2017/06/28/the_justices_lay_down_the_law_414173.html" +"originalLink": "https://t.co/y0BkZIKyc1" +"guid": "100414173" +"category": "PM Update" +"author": SimpleXMLElement {#374} } SimpleXMLElement {#370} SimpleXMLElement {#370 ▼ +0: SimpleXMLElement {#377} } 

The media and author nodes are empty, although you can see in the source code that they matter ... How to get the value of these fields? Is there something better than SimpleXml?

    2 answers 2

    Use SimpleXml. Just pay attention to the namespace media :.

     $content_data = @file_get_contents("text.xml"); $_ = simplexml_load_string($content_data); foreach($_->item as $nextXmlRow) { var_export((string)$nextXmlRow->author); var_export($nextXmlRow->children('media', true)); } 

    HTML code:

     <a href="/authors/david_rivkin" data-mce-href="../../authors/david_rivkin">Rivkin</a> &amp; <a href="/authors/lee_casey" data-mce-href="../../authors/lee_casey">Casey</a>, Wall Street Journal' SimpleXMLElement::__set_state(array( 'content' => SimpleXMLElement::__set_state(array( )), 'thumbnail' => SimpleXMLElement::__set_state(array( )), 'title' => 'The Justices Lay Down the Law', )) 

      I repeat. media is a namespace. The content of this space (node) is $nextXmlRow->children('media', true) . Node attributes are available via the attributes() method. To get a string value, we use a type conversion (string). The documentation for SimpleXml is all detailed.