Jump to content

how to read xml with php


piyush23424

Recommended Posts

hi,

 

i am requesting bookrenter.com with curl and getting back a response in something like xml format

 

The response is

<response>
  <book>
    <info>
      <isbn13>9780324786453</isbn13>
      <isbn10>032478645X</isbn10>
    </info>
    <prices>
      <rental_price days="45">$17.43</rental_price>
      <rental_price days="90">$24.35</rental_price>
      <rental_price days="60">$20.87</rental_price>
      <rental_price days="30">$15.87</rental_price>
      <rental_price days="125">$28.99</rental_price>
    </prices>
    <lowest_shipping_price>$0.00</lowest_shipping_price>
    <availability>In Stock</availability>
    <url>http://www.shareasale.com/r.cfm?u=&b=96706&m=14293&urllink=www.bookrenter.com%2Fdiscovering-computers-2010-living-in-a-digital-world-complete-shelly-cashman-series-032478645X-9780324786453</url>
  </book>
</response>

 

 

Now i want to  process the response so that it will show the values in user readable form  like

 

45 day's rent is $17.43

90 day's rent is $24.35

60 day's rent is $20.87

30 day's rent is $15.87

125 day's rent is $28.99

 

Shipping price is $0.00

 

plz help

 

Thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/219058-how-to-read-xml-with-php/
Share on other sites

<?php
$str = <<< XML
<response>
  <book>
      <info>
            <isbn13>9780324786453</isbn13>
            <isbn10>032478645X</isbn10>
      </info>
      <prices>
             <rental_price days="45">$17.43</rental_price>
             <rental_price days="90">$24.35</rental_price>
             <rental_price days="60">$20.87</rental_price>      
             <rental_price days="30">$15.87</rental_price>      
             <rental_price days="125">$28.99</rental_price>    
      </prices>    
      <lowest_shipping_price>$0.00</lowest_shipping_price>    
      <availability>In Stock</availability>    
      <url>
      http://www.shareasale.com/r.cfm?u=&b=96706&m=14293&urllink=www.bookrenter.com%2Fdiscovering-computers-2010-living-in-a-digital-world-complete-shelly-cashman-series-032478645X-9780324786453
      </url>  
</book>
</response>
XML;

$xml = simplexml_load_string($str);
foreach ($xml->book->prices->rental_price as $p) {
    echo "{$p['days']} day's rent is  $p<br />";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.