0

I have a file I download from my supplier, products.xml. I have a script to take data from this file and put it in a csv file.

So I'm looking at this script and there appears to be data pulled from the xml that I can't see with any xml editor. Like images and categories for example. While on the other hand, I see certain fields in the xml file that aren't being put in the csv when they should be, like the product name for instance.

It does everything right except add the name of the product and the description.

Here is where the script is grabbing data from the xml into an array.

foreach($xmlData->children() as $product){
// Create an array which holds all of the data merged from the xml file
    $filteredData = array('code' => (string)$product->sku
                     ,'name' => (string)$product->name
                     ,'brand' => (string)$product->manufacturer
                     ,'description' => (string)$product->long_description
                     ,'costPrice' => (string)$product->price
                     ,'price' => (((string)$product->price) * 2)
                     ,'freeShipping' => 0
                     ,'weight' => (string)$product->weight
                     ,'allowPurchases' => 1
                     ,'productVisible' => 1
                     ,'productAvailability' => 'Ships within 48 Hours'
                     ,'trackInventory' => 1
                     ,'stockLevel' => (string)$product->stock_quantity
                     ,'lowStockLevel' => 1
                     ,'productCondition' => 'New'
                     ,'upc' => (string)$product->barcode
                     ,'category' => '');

// First category name goes to category details field
// If more than one category name exists, last category name goes to category field
$filteredData['categoryDetails'] = (string)$product->categories->category[0];
$categoryCount = count($product->categories->children());
if($categoryCount > 1){
    $filteredData['category'] = (string)$product->categories->category[$categoryCount - 1];
}

// Product image links must be appended to: http://images.xyz.com/product_images
$staticLinkToPrepend = 'http://images.xyz.com/product_images';
$filteredData['images'] = array();
foreach($product->images->children() as $image){
    $filteredData['images'][] = $staticLinkToPrepend . ((string)$image);
}

In the products.xml file I see no reference for categories at all yet the code written produces correct categories in the csv file. In the xml file I see no reference at all to the images but this code produces correct image listings for each product. This is all great but I need to understand why this is. Once I can understand this, I can probably figure out why the name and product description part isn't working.

Also he used just "description" for the product description but in the xml file, it has long_description and short_description. Neither of those work either. Name is listed as is in the xml file so I'm not sure why it's not working.

Array
(
[product] => Array
    (
        [0] => Array
            (
                [@attributes] => Array
                    (
                        [sku] => CATDJ
                        [type] => BR
                        [vendor] => DJ
                        [active] => 0
                        [on_sale] => 0
                        [discountable] => 0
                    )

                [name] => DJ CATALOGS
                [short_description] => Array
                    (
                    )

                [long_description] => Array
                    (
                    )

                [price] => 1.5
                [stock_quantity] => 0
                [release_date] => 2003-05-06T00:00:00-04:00
                [barcode] => 782421712315
            )

        [1] => Array
            (
                [@attributes] => Array
                    (
                        [sku] => CATPD
                        [type] => DISC
                        [vendor] => PIEDR
                        [active] => 0
                        [on_sale] => 0
                        [discountable] => 0
                    )

                [name] => PIE DRUM CATALOG
                [short_description] => Array
                    (
                    )

                [long_description] => Array
                    (
                    )

                [price] => 1.5
                [stock_quantity] => 0
                [release_date] => 2003-05-06T00:00:00-04:00
                [barcode] => 603912241230
            )

    )

)

I just took the xml file and cut it down to only two products. I use the following to get the above.

if(!file_exists($xmlFileName)){
die('* XML file path is incorrect.  Please change and try again.');
}
$xmlData = simplexml_load_file($xmlFileName);
$json = json_encode($xmlData);
$array = json_decode($json,TRUE);
print "<pre>";
print_r($array);
print "</pre>";

So "name" is there but it won't show up in the final csv. long_description and short_description are there but look empty using the above but they are definitely in the xml file with text values.

I was able to create another script based on this one for my Share-A-Sale feed but that data was coming from the database and I had no issues with it. But I've been at this one for two weeks. I'm remodeling my site now which is why I needed it in the first place.

This is what a sample entry looks like in the xml file.

<products>
<product sku="CATDJ" type="BR" vendor="DJ" active="0" on_sale="0" discountable="0">
<name>DJ CATALOGS</name>
<short_description><![CDATA[some short description]]></short_description>
<long_description><![CDATA[some long description]]></long_description>
<price>1.5</price>
<stock_quantity>0</stock_quantity>
<release_date>2003-05-06T00:00:00-04:00</release_date>
<barcode>782421712315</barcode>
</product>
</products>

UPDATE: Now I'm really confused. I dug out an old version of the products.xml file from a backup to see if the vendor changed the xml file. It is much different and I can see many more fields (see below) but I'm still confused as to how the current products file that doesn't have the image and category data listed can still produce the data in the csv file.

 <product active="1" on_sale="0" discountable="0">
 <sku>AL1012XO</sku>
 <name>
  <![CDATA[LACE UP GLOVES]]>
</name>
<description>
  <![CDATA[Able to be adjusted as tight as you desire with a lace-and-grommet closure, they extend over the tops of the hands and have a built-in finger loop for a sleek look.]]>
</description>
<keywords></keywords>
<price>10.100000</price>
<stock_quantity>34</stock_quantity>
<reorder_quantity>0</reorder_quantity>
<height>0.000000</height>
<length>0.000000</length>
<diameter>0.000000</diameter>
<weight>0.000000</weight>
<color>Black</color>
<material>cotton</material>
<barcode>883125011270</barcode>
<release_date>2008-11-10</release_date>
<images>
  <image>/AL1012XO/AL1012XO.jpg</image>
  <image>/AL1012XO/AL1012XOB.jpg</image>
</images>
<categories>
  <category code="166" video="0" parent="172">Clothing</category>
  <category code="172" video="0" parent="">Clothing</category>
  <category code="641" video="0" parent="172">Accessories</category>
  <category code="167" video="0" parent="1001">Costumes</category>
</categories>
<manufacturer code="AL" video="0">Golvers</manufacturer>
<type code="AC" video="0">AC</type>

UPDATE: I think I'm getting there. I still don't know why I can't see everything in the xml file but it's there nonetheless. The csv is just as I want it BUT I'm still unable to get the name and description in the csv file. On a good note, the references for the name and description are correct and print the data fine using print_r but it appears the problem is getting that data into the csv file. It appears those two fields are the only two with [CDATA[ so I'm looking into why CDATA fields aren't making it into the csv file. I'm using this now too.

$xmlData = simplexml_load_file($xmlFileName, null, LIBXML_NOCDATA);

I've given up. I've scoured this site and the web and feel like I've tried everything. I don't understand why the "name" and "description" data will not get written to the csv file when everything else is fine. Name and description in the xml file seems pretty straight forward. I know this script was working over a year ago, I don't know why it's not working only for those two things now. :(

T. Sanders
  • 33
  • 7
  • You probably have to check what `categories` and `images` are in the class that `$product` is an instance of. – duplode May 05 '14 at 21:10
  • I don't know what you mean. I don't see categories or images anywhere in the xml which is the mystery to me. I need to see if I can dig up more info on xml format. It seemed pretty straight forward to me. :) – T. Sanders May 06 '14 at 01:05
  • I think I understand now why I can't see all the xml data. I'm still working on how to access the name and description info. I think I will find an answer soon. IMSoP's answer was very helpful here: http://stackoverflow.com/questions/18667478/simplexml-load-string-not-parsing-my-xml-string-charset-issue/18679115#18679115 – T. Sanders May 06 '14 at 19:48

2 Answers2

0

You want to use LIBXML_NOCDATA. This works for me using your sample data:

$xmlData = simplexml_load_string($xml, null, LIBXML_NOCDATA);    
echo $xmlData->product->name."\n";
echo $xmlData->product->short_description."\n";
echo $xmlData->product->long_description."\n";

Output:

DJ CATALOGS
some short description
some long description
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Thanks Abra. This did give me a better looking output but I'm still getting blank columns only for name and description. I'll keep trying. – T. Sanders May 06 '14 at 01:06
0

My supplier is going to be the death of me. They have 3 different xml links all claiming to be the entire catalog. They all aren't. I was pulling from an old link. I still don't know why it wouldn't put the name and description in the csv since those fields were there. But I used the one listed above that I felt had hidden fields (it didn't) and yes, that one is missing most of the useful product data. Went to at least look at a different xml file they said had images and it has all of the info I need. All of that suffering for nothing. :)

T. Sanders
  • 33
  • 7