austinh
austinh

Reputation: 1071

How would I use XMLReader to display an image?

I have an XML document that I need to load and in the document it has several image sizes. But, I only need the medium image URL returned. How would I go about getting that?

This is the XML code:

<?xml version="1.0" encoding="utf-8"?>
<ipl status="">
<sizes>
    <image size="small">s/64654587.jpg</image>
    <image size="medium">m/64654587.jpg</image>
    <image size="large">l/64654587.jpg</image>
</sizes></ipl>

Upvotes: 0

Views: 141

Answers (1)

dfsq
dfsq

Reputation: 193261

With SimpleXML things are easy:

$xml = simplexml_load_string($str);
$img = $xml->xpath('//image[@size="medium"]');

$medium = (string)$img[0];

echo $medium; // Output: m/64654587.jpg

If you need to read XML from the file use simplexml_load_file instead.

http://php.net/manual/en/function.simplexml-load-string.php
http://php.net/manual/en/function.simplexml-load-file.php

Upvotes: 3

Related Questions