?
Solved

PHP SimpleXML finding a specific Attribute

Posted on 2009-02-08
4
Medium Priority
?
537 Views
Last Modified: 2013-11-10
I am using the Flickr API.  The XML below is loaded into an object called $flickrResponse using simplexml_load_string();

I need the source value of the size element where the label is "Thumbnail" (http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg).  I tried the loop below...also tried a for each loop, but it's not working out.  Is there a way to do it without creating a loop?

Thanks in advance for any help with this!
Alex
<rsp stat="ok">
<sizes canblog="1" canprint="1" candownload="1">
<size label="Square" width="75" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_s.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/sq/"/>
<size label="Thumbnail" width="100" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/t/"/>
<size label="Small" width="240" height="180"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_m.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/s/"/>
<size label="Medium" width="500" height="375"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/m/"/>
<size label="Original" width="640" height="480"
      source="http://farm2.static.flickr.com/1103/567229075_6dc09dc6da_o.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/o/"/>
</sizes>
</rsp>
 
	while ($flickrResponse) {
	
		if ($flickrResponse->sizes->size[label] = "Thumbnail") {
		
			echo $flickrResponse->sizes->size[label];
		
			$photoThumb = $flickrResponse->sizes->size[source];
		}
	}

Open in new window

0
Comment
Question by:alexswebsite
  • 2
4 Comments
 
LVL 20

Expert Comment

by:thehagman
ID: 23588803
If you also have the XML string, then a pattern match helps
$xmlstring = '<rsp stat="ok"><sizes ... </rsp>';
if (preg_match('/<size label="Thumbnail"[^>]+source="([^"]+)"/', $xmlstring, $matches) {
  echo  $matches[1];
} else {
  echo "No thumbnail";
}

Open in new window

0
 
LVL 20

Accepted Solution

by:
thehagman earned 2000 total points
ID: 23588839
Within SimpleXML the solution should be something like


foreach ($flickrresponse->sizes->children() as $size) {
   if ($size[label] == 'Thumbnail') {
      $photoThumb = $size[source];
   }
}

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 23591346
Here is what I would do.  Object iterators using foreach() are very efficient.  HTH, ~Ray
<?php // RAY_xml_flickr.php
 
// TEST DATA
$xml = '<rsp stat="ok">
<sizes canblog="1" canprint="1" candownload="1">
<size label="Square" width="75" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_s.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/sq/"/>
<size label="Thumbnail" width="100" height="75"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_t.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/t/"/>
<size label="Small" width="240" height="180"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01_m.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/s/"/>
<size label="Medium" width="500" height="375"
      source="http://farm2.static.flickr.com/1103/567229075_2cf8456f01.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/m/"/>
<size label="Original" width="640" height="480"
      source="http://farm2.static.flickr.com/1103/567229075_6dc09dc6da_o.jpg"
      url="http://www.flickr.com/photos/stewart/567229075/sizes/o/"/>
</sizes>
</rsp>';
 
// MAKE AN OBJECT FROM THE XML
$obj = SimpleXML_Load_String($xml);
 
// ITERATOR
foreach ($obj->sizes->size as $my_image)
{
 
// NOTE THE USE OF THE ASSOCIATIVE ARRAY INSIDE THE 'size' FIELD
   if ($my_image["label"] == "Thumbnail")
   {
      echo "<br/>{$my_image["source"]}\n";
   }
// END ITERATOR
}

Open in new window

0
 

Author Closing Comment

by:alexswebsite
ID: 31544425
This solution was perfect--concise and quick.  Thanks to everyone for the help!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question