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
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 />";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.