Example returned XML file

When i convert the data from Filemaker to XML and if i want to import it in Woocommerce with the plugin All Importer the xml doesnt have valid fields, how can i match the field name’s to the data fields? I tried to use simplexml to read the file and change the field names but with no success.
When i run this script i get only one result, i tried to dump the $this->data->RESULTSET there i get all the data in the XML but when i try to loop the $this->data->RESULTSET->ROW it look’s like ROW is empty.

My code:

<?php
error_reporting(E_ALL);

if (!ini_get('display_errors'))
{
    ini_set('display_errors', '1');
}

class Converter

{

    public function array2xml($array, $node_name = "xml")
    {
        $dom = new DOMDocument('1.0', 'UTF-8');
        $dom->formatOutput = true;
        $root = $dom->createElement($node_name);
        $dom->appendChild($root);
        $array2xml =
            function ($node, $array) use($dom, &$array2xml)
            {
                foreach($array as $key => $value)
                {
                    if (is_array($value))
                    {
                        $n = $dom->createElement($key);
                        $node->appendChild($n);
                        $array2xml($n, $value);
                    }
                    else
                    {
                        $attr = $dom->createAttribute($key);
                        $attr->value = htmlspecialchars($value);
                        $node->appendChild($attr);
                    }
                }
            };
        $array2xml($root, $array);
        return $dom->saveXML();
    }

    public function clean_explode($seperator, $string)
    {
        $result = trim($string, ' ' . $seperator);
        $result = explode($seperator, $result);
        $result = array_filter($result, 'strlen'); // removes double seperators
        $result = array_map('trim', $result);
        return $result;
    }

    public function sanitizer($text)
    {
        $text = preg_replace('~[^pLd]+~u', '-', $text);
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        $text = preg_replace('~[^-w]+~', '', $text);
        $text = trim($text, '-');
        $text = preg_replace('~-+~', '-', $text);
        $text = strtolower($text);
        if (empty($text))
        {
            return 'n-a';
        }

        return $text;
    }

    public function import($file)
    {
        $this->data = simplexml_load_file($file);
        return $this->data;
    }

    public function prepare()
    {
        foreach($this->data->RESULTSET as $xml_products)
        {
            foreach($xml_products->ROW as $xml_product){
            $product = new STDClass();
            $product->sku = (string)$xml_product->COL[0]->DATA;
            $product->brand = array_map([$this, 'sanitizer'], $this->clean_explode(':', (string)$xml_product->COL[1]->DATA));
            $product->name = (string)$xml_product->COL[2]->DATA;
            $product->description = (string)$xml_product->COL[3]->DATA;
            $product->price = str_replace(array(
                '.',
                ','
            ) , array(
                '',
                '.'
            ) , (string)$xml_product->COL[4]->DATA);
            $product->weight = (string)$xml_product->COL[5]->DATA;
            $sizes_a = array_map([$this, 'sanitizer'], $this->clean_explode(':', (string)$xml_product->COL[6]->DATA));
            $sizes_b = array_map([$this, 'sanitizer'], $this->clean_explode(':', (string)$xml_product->COL[7]->DATA));
            $product->sizes = array_unique(array_merge($sizes_a, $sizes_b));
            $colors_a = array_map([$this, 'sanitizer'], $this->clean_explode("n", (string)$xml_product->COL[8]->DATA));
            $colors_b = array_map([$this, 'sanitizer'], $this->clean_explode("n", (string)$xml_product->COL[9]->DATA));
            $product->colors = array_unique(array_merge($colors_a, $colors_b));
            $product->tags = array_map([$this, 'sanitizer'], $this->clean_explode(',', (string)$xml_product->COL[10]->DATA));
            $product->categories = array();
            for ($i = 11; $i < $xml_product->count(); $i++)
            {
                $categories = array_map([$this, 'sanitizer'], $this->clean_explode(',', (string)$xml_product->COL[$i]->DATA));
                $product->categories = array_unique(array_merge($product->categories, $categories));
            }

            if (isset($this->products[$product->name]))
            {
                $existing_product = $this->products[$product->name];
                $existing_product->categories = array_unique(array_merge($existing_product->categories, $product->categories));
                $this->products[$product->name] = $existing_product;
            }
            else
            {
                $this->products[$product->name] = $product;
            }
                return $product;

            }

        }

    }



    public function export()
    {
        $xml_product_ids = array();
        if (count($this->products) > 0)
        {
            foreach($this->products as $xml_product)
            {
                $xml_product_ids[] = $xml_product->sku;
                $product = !empty($products) ? $products[0] : null;
                $update = $product != null;
                if (!$update)
                {
                    $product = array(
                        'post_title' => $xml_product->name,
                        'post_name' => $xml_product->name,
                        'post_content' => '',
                        'post_excerpt' => '',
                        'post_status' => 'publish',
                        'post_author' => 1,
                        'post_type' => 'product'
                    );
                    return $this->array2xml($product);
                }
            }
        }
    }
}

$test = new Converter();
$dump = $test->import('../data/karoshi.xml');
$test->prepare();
print_r($test->export());
?>

Read more here: Filemaker XML parser


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: