2 Replies - 714 Views - Last Post: 02 April 2012 - 03:53 AM Rate Topic: -----

#1 Polofiesta  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 78
  • Joined: 15-December 10

Retrieve data from XML specific tag?

Posted 01 April 2012 - 06:55 PM

Hi so I have an xml file i get from amazon and I want to get the data from the <DetailedPageURL> tag in this xml code:
<Item><ASIN>0679728759</ASIN><DetailPageURL>http://www.amazon.com/Blood-Meridian-Evening-Redness-West/dp/0679728759%3FSubscriptionId%3DAKIAJ24PUSWUYFBPDUHA%26tag%3Dtextbexcha00-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0679728759</DetailPageURL><ItemLinks>


How would i do this through php so i can just get that link to the book? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve data from XML specific tag?

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 789
  • View blog
  • Posts: 1,675
  • Joined: 30-January 09

Re: Retrieve data from XML specific tag?

Posted 01 April 2012 - 07:08 PM

You can use the SimpleXMLElement object to achieve this:
$xmlString = file_get_contents('path/to/my/xml/file');
$xml = new SimpleXMLElement($xmlString);

foreach($xml->Item as $item)
{
  foreach($item->DetailPageURL as $detailPageUrl)
  {
    // Cast as whatever type you need here.  For URL it will be string
    $url = (string)$detailPageUrl;
    // Do your code here that deals with this URL
  }
}


The code above loops through the XML structure as a whole, mind you, so if you have multiple Items or multiple DetailPageURLs, you will hit each of those.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,415
  • Joined: 23-August 08

Re: Retrieve data from XML specific tag?

Posted 02 April 2012 - 03:53 AM

Alternatively, use XPath.

Example:

<?php

$xml = simplexml_load_file('sxml_ex.xml');
$id = 12345;
// Get the Item with Id 12345
$myNode = $xml->xpath('/Items/Item/Id[.="' . $id . '"]/parent::*');
print("Id $id's name is {$myNode[0]->Name}\n");
?>


<?xml version="1.0"?>
<Items>
   <Item>
      <Id>12345</Id>
      <Name>Item 1</Name>
   </Item>
   <Item>
      <Id>54321</Id>
      <Name>Item 2</Name>
   </Item>
</Items>

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1