M
M
Michael2016-05-18 19:33:14
PHP
Michael, 2016-05-18 19:33:14

How to extract values ​​from XML?

Hello!
There is this repeating XML structure:

<entry>
<author>
      <name>Dunnock_D</name>
      <uri>http://www.flickr.com/people/dunnock_d/</uri>
 </author>
    <link rel="license" type="text/html" href="https://creativecommons.org/licenses/by-nc/2.0/deed.en" />
    <link rel="enclosure" type="image/jpeg" href="http://farm8.staticflickr.com/7548/26820724620_1d221c3187_b.jpg" />
</entry>

I need to take the href value only where rel="enclosure" (get a link to the image).
I do this in a loop:
$xml = simplexml_load_string($result);

    foreach ($xml->entry as $pixinfo) {
       
       echo $pixinfo->link[1]['href'];
       
    }

But in this structure, sometimes two, sometimes one link value. And when I take through the array index, sometimes it comes across not what I need.
That is, I need to accurately select only link rel="enclosure"
Is there a concise solution or do I need to run additional checks for this in a loop?
Read the documentation, did not help.
Thank you in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2016-05-18
@Mikhail_RU

I'm not really into php, but I think something like this:
$xml->xpath("//link[@rel='enclosure']");

Y
ya_yshel_rabotati_v_teleg, 2016-05-18
@ya_yshel_rabotati_v_teleg

at least you can use preg_match_all

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question