1

I've been creating a simple website for a client (I'm a designer with front-end skills) but I've been thrown a curve ball and now they want a catalogue of products imported via a CSV or XML file. I've chosen the XML option as I have even less of a clue what to do with the CSV.

Would love some guidance on how I can start pulling out the XML using PHP.

XML product example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<style body="Neck%2C+waist+and+cuff+ribs+in+cotton%2FLycra%C2%AE%0ATwin+needle+stitching+throughout%0ATaped+back+neck%0A" brand="Fruit+of+the+Loom" code="SS8" date="20140303162801" footnote="Garment+sizes+are+approximate+and+for+guidance+only%0A" material="80%25+cotton+Belcoro%C2%AE+yarn%2F+20%25+polyester." name="Fruit+of+the+Loom+Raglan+Sweatshirt" type="Adult" weight="280+gsm">
<sizes>
    <row name="Size:">
        <col value="S"/>
        <col value="M"/>
        <col value="L"/>
        <col value="XL"/>
        <col value="XXL"/>
    </row>
    <row name="Chest (to fit):">
        <col value="35%2F37"/>
        <col value="38%2F40"/>
        <col value="41%2F43"/>
        <col value="44%2F46"/>
        <col value="47%2F49"/>
    </row>
</sizes>
<skus>
    <sku colour="BLK">
        <colours>
            <colour hex="0F1315" name="Black"/>
        </colours>
        <sizes>
            <size cartonPrice="490" cartonSize="36" name="S" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="M" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="L" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="XL" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="24" name="XXL" packPrice="520" packSize="6" singlePrice="560"/>
        </sizes>
    </sku>
    <sku colour="BOT">
        <colours>
            <colour hex="15451A" name="Bottle"/>
        </colours>
        <sizes>
            <size cartonPrice="490" cartonSize="36" name="S" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="M" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="L" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="36" name="XL" packPrice="520" packSize="6" singlePrice="560"/>
            <size cartonPrice="490" cartonSize="24" name="XXL" packPrice="520" packSize="6" singlePrice="560"/>
        </sizes>
    </sku>

....

flangofas
  • 332
  • 1
  • 5
  • 15
sarah3585
  • 637
  • 13
  • 37
  • 1
    http://us1.php.net/manual/en/book.simplexml.php – Digital Chris Mar 10 '14 at 13:12
  • It depends on how you want this to work. Do you want an upload form, or will the CSV/XML file be supplied another way (and from your program's perspective, can just be found on disk)? When you say import, do you mean you wish to copy CSV/XML rows into a database? – halfer Mar 10 '14 at 13:13
  • I need to display the attributes like the Sizes available (S,M,L,XL,XXL) into the front-end. – sarah3585 Mar 10 '14 at 13:25
  • 1
    Use simplexml as @DigitalChris mentioned. The best method that I found is to store this all in a temporary database and then run through it again. This will give you more flexibility when dealing with the content. – Tony M Mar 10 '14 at 13:26
  • Ah right, if there is no need for the client to upload XML files, you can just store it on disk and read it via SimpleXML, as per Sanjay's answer. @Tony is right that you can load it into a database, but I suspect you want the simplest solution possible! – halfer Mar 10 '14 at 14:01

2 Answers2

4

Please refer following code I think this will help you.

<!-- suppose this is book.xml file -->
<?xml version="1.0"?>
<catalog>
<book id="bk101">
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications
  with XML.</description>
   </book>
   <book id="bk102">
  <author>Ralls, Kim</author>
  <title>Midnight Rain</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-12-16</publish_date>
  <description>A former architect battles corporate zombies,
  an evil sorceress, and her own childhood to become queen
  of the world.</description>
   </book>
<catalog>

And this is your PHP:

<?php
// Loading the XML file
$xml = simplexml_load_file("books.xml");
echo "<h2>".$xml->getName()."</h2><br />";
foreach($xml->children() as $book)
{
    echo "BOOK : ".$book->attributes()->id."<br />";
    echo "Author : ".$book->author." <br />";
    echo "Title : ".$book->title." <br />";
    echo "Genre : ".$book->genre." <br />";
    echo "Price : ".$book->price." <br />";
    echo "Publish Date : ".$book->publish_date." <br />";
    echo "Description : ".$book->description." <br />";
    echo "<hr/>";
}
?>
halfer
  • 19,824
  • 17
  • 99
  • 186
Sanjay
  • 1,958
  • 2
  • 17
  • 25
  • Thank you, this has given me something to play with. The XML I'm having to uses attributes a lot, and I can't get this working using: ".$xml->getName()."
    "; foreach($xml->children() as $product) { echo "Sizes : ".$product->sizes->row->col->attributes()->value."
    "; echo "Colour : ".$product->skus->sku->attributes()->color."
    "; } ?>
    – sarah3585 Mar 10 '14 at 14:50
1

Of course you can use DOM in PHP. Given the books example, reading data into PHP:

<!-- suppose this is book.xml file -->
<?xml version="1.0"?>
<catalog>
<book id="bk101">
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications
  with XML.</description>
   </book>
   <book id="bk102">
  <author>Ralls, Kim</author>
  <title>Midnight Rain</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-12-16</publish_date>
  <description>A former architect battles corporate zombies,
  an evil sorceress, and her own childhood to become queen
  of the world.</description>
   </book>
<catalog>

Example: https://eval.in/117319

$dom = new DOMDocument();
$dom->loadXml('book.xml');
$xpath = new DOMXpath($dom);

$result = [];
foreach ($xpath->evaluate('//book') as $book) {
  $result[] = [
    'id' => $xpath->evaluate('string(@id)', $book),
    'Author' => $xpath->evaluate('string(author)', $book),
    'Title' => $xpath->evaluate('string(title)', $book),
    'Genre' => $xpath->evaluate('string(genre)', $book),
    'Price' => $xpath->evaluate('number(price)', $book),
    'Publish Date' => $xpath->evaluate('string(publish_date)', $book),
    'Description' => $xpath->evaluate('string(description)', $book)
  ];
}
var_dump($result);
ThW
  • 19,120
  • 3
  • 22
  • 44