Jump to content

Simple XML Help


bravo14

Recommended Posts

Hi Guys

 

I have an XML feed that produces the following data:-

 

<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
- <bookCategories>
- <category>
  <id>addressbooks</id> 
  <name>Address Books & Journals</name> 
  </category>
- <category>
  <id>art</id> 
  <name>Art, Architecture & Photography</name> 
  </category>
- <category>
  <id>audiocd</id> 
  <name>Audio Books</name> 
  </category>
- <category>
  <id>audiocassette</id> 
  <name>Audio Cassettes</name> 
  </category>
- <category>
  <id>bestsellers</id> 
  <name>Best Sellers</name> 
  </category>

 

I want to list each of the category names

 

I am using the code below:

 

   <?php
       //category listing
         $file="http://book.find-services.co.uk/bookCategories.aspx?site=Purelybooks";
         $dom=new DOMdocument();
         $dom->load($file);
         $xml=simplexml_import_dom($dom);
         echo $xml->category;
      ?>

 

The categories are not showing at all, there are no errors being produced.

 

Hopefully someone can give me some pointers in the right direction.

Link to comment
https://forums.phpfreaks.com/topic/263311-simple-xml-help/
Share on other sites

<?php
$xml = simplexml_load_file('bookCats.xml');
foreach($xml->xpath('//name') as $cat) {
    echo $cat . '<br />';
}
?>

 

Results :

 

Address Books & Journals

Art, Architecture & Photography

Audio Books

Audio Cassettes

Best Sellers

 

Link to comment
https://forums.phpfreaks.com/topic/263311-simple-xml-help/#findComment-1349445
Share on other sites

just spotted your revised requirement for links

 

<?php
$xml = simplexml_load_file('bookCats.xml');
echo "<ul>\n";
foreach($xml->xpath('//category') as $cat) {
    echo "<li><a href='category.php?id=$cat->id'>$cat->name</a></li>\n";
}
echo "</ul>\n";
?>

 

results

<ul>
<li><a href='category.php?id=addressbooks'>Address Books & Journals</a></li>
<li><a href='category.php?id=art'>Art, Architecture & Photography</a></li>
<li><a href='category.php?id=audiocd'>Audio Books</a></li>
<li><a href='category.php?id=audiocassette'>Audio Cassettes</a></li>
<li><a href='category.php?id=bestsellers'>Best Sellers</a></li>
</ul>

Link to comment
https://forums.phpfreaks.com/topic/263311-simple-xml-help/#findComment-1349447
Share on other sites

That works perfectly, however using the same layout to generate another dataset I am getting the following error

 

Parse error: syntax error, unexpected T_VARIABLE in /home/sites/purelybooks.com/public_html/includes/best_sellers_inc.php on line 10

 

The code is

  <?php
       //category listing
         $best_sellers_file = "http://book.find-services.co.uk/bookSearch.aspx?site=purelybooks&category=newreleases&sort=popular&pagesize=10";
echo ('<div class="jcarousel-skin-name">
  <div class="jcarousel-container">
    <div class="jcarousel-clip">
      <ul class="jcarousel-list">');
         $best_sellers_xml = simplexml_load_file($best_sellers_file);
         foreach($best_sellers_xml->xpath('//book') as $best_sellers_book) {
		 var_dump $best_sellers_book;
         echo ('<li class="jcarousel-item">'$best_sellers_book->title'</li>\n');
}
echo('</ul>
    </div>
</div>
</div>');
      ?>

 

The xml file I am reading looks like

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<books>-<book><id>0007466072</id><isbn13>9780007466078</isbn13><asin>0007466072</asin><title>A Song of Ice and Fire (5) - A Dance With Dragons: Part 2 After the Feast</title><numberOfPages>592</numberOfPages>-<authors><author>George R. R. Martin</author></authors><published>15 March 2012</published><review/><rrp>8.99</rrp><publisher>Harper Voyager</publisher><cover>Paperback</cover><imageURL>http://images-eu.amazon.com/images/P/0007466072.02.MZZZZZZZ.jpg</imageURL><thumbnailURL>http://images-eu.amazon.com/images/P/0007466072.02.THUMBZZZ.jpg</thumbnailURL><pricesLastCached>30/05/2012 05:06:10</pricesLastCached><minimumPrice>3.86</minimumPrice></book>-<book><id>0007466064</id><isbn13>9780007466061</isbn13><asin>0007466064</asin><title>A Song of Ice and Fire (5) - A Dance With Dragons: Part 1 Dreams and Dust</title><numberOfPages>704</numberOfPages>-<authors><author>George R. R. Martin</author></authors><published>15 March 2012</published><review/><rrp>8.99</rrp><publisher>Harper Voyager</publisher><cover>Paperback</cover><imageURL>http://images-eu.amazon.com/images/P/0007466064.02.MZZZZZZZ.jpg</imageURL><thumbnailURL>http://images-eu.amazon.com/images/P/0007466064.02.THUMBZZZ.jpg</thumbnailURL><pricesLastCached>29/05/2012 18:00:50</pricesLastCached><minimumPrice>3.86</minimumPrice></book>

Link to comment
https://forums.phpfreaks.com/topic/263311-simple-xml-help/#findComment-1349726
Share on other sites

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.