0
<resources>
<merchants count="7">
<merchant id="1300" name="Wal-M" count="1" logo_url="" url=""/>
<merchant id="1387" name="Nothing.com" count="1" logo_url="" url=""/>
<merchant id="3486" name="Micro" count="1" logo_url="" url=""/>
<merchant id="13880" name="Sea" count="1" logo_url="" url=""/>
<merchant id="13881" name="Kma" count="1" logo_url="" url=""/>
<merchant id="14711" name="Cas Interstate Music" count="1" logo_url="" url=""/>
<merchant id="1882969" name="Targ" count="1" logo_url="" url="http://r.url.com"/>
</merchants>

I have this xml file which return random number of merchant's every API call I always need the url attribute from the merchant with the name Targ

my code:

$fetchurl = "API URL"; $xml = simplexml_load_file($fetchurl); $merchantInfo=$xml->resources->merchant; <---stuck here how to get the url of Targ?

zumbamusic
  • 180
  • 1
  • 2
  • 14
  • Possible duplicate: http://stackoverflow.com/questions/1256796/how-to-get-the-value-of-an-attribute-from-xml-file-in-php Also, try to research a lot before coming to Stackoverflow for an answer. – Always Learning Forever Jun 28 '14 at 13:31
  • @AkshatTripathi I have read this answer I can't still make it work. I can get all attributes but how about the url for Targ I can get the name Targ but i dont need that – zumbamusic Jun 28 '14 at 13:35
  • In that case, I think that a possible solution might to actually use a conditional statement to see whether you have the attribute you were looking for, and then, if it's the desired attribute, then get its URL. For example, `if ($xml->element::attr == "desired value") { echo $xml->element::another_attr}` – Always Learning Forever Jun 28 '14 at 13:37
  • also not a duplicate that question has just 1 value and he seeks to get `count="7"` in my example I need the merchant value no merchants count – zumbamusic Jun 28 '14 at 13:38
  • @AkshatTripathi I will always will have that attribute this is why I need to get it by name – zumbamusic Jun 28 '14 at 13:39
  • So, ya, I think that it should work, right? you can see if the variable --in which you've got the attribute's value-- is equal to the desired value and if yes, then get its URL – Always Learning Forever Jun 28 '14 at 13:40
  • Try this :http://www.php.net/manual/en/simplexml.examples-basic.php – Always Learning Forever Jun 28 '14 at 13:41
  • @AkshatTripathi I want a better way than a loop, any alternatives? I got the value name should be something more efficient – zumbamusic Jun 28 '14 at 13:47
  • Try the answer given by MarioD'Boro – Always Learning Forever Jun 28 '14 at 13:49

1 Answers1

0

First off, you're xml isn't valid. It's missing the root closing </resources>.

Second you can just use SimpleXMLElement and use xpath and target Targ:

$data = '<resources><merchants count="7"><merchant id="1300" name="Wal-M" count="1" logo_url="" url=""/><merchant id="1387" name="Nothing.com" count="1" logo_url="" url=""/><merchant id="3486" name="Micro" count="1" logo_url="" url=""/><merchant id="13880" name="Sea" count="1" logo_url="" url=""/><merchant id="13881" name="Kma" count="1" logo_url="" url=""/><merchant id="14711" name="Cas Interstate Music" count="1" logo_url="" url=""/><merchant id="1882969" name="Targ" count="1" logo_url="" url="http://r.url.com"/></merchants></resources>';
$xml = new SimpleXMLElement($data);
$url = (string) $xml->xpath('//merchant[@name="Targ"]')[0]->attributes()->url;
echo $url; // http://r.url.com
  • thank you helped me a lot! I seperated $url to 2 parts just to make sure xpath finds anything before I try to get attributes or there will be errors ;) – zumbamusic Jun 28 '14 at 14:58