title: Filemaker XML parser
body: <p><a href="https://i.stack.imgur.com/ukAOy.png" rel="nofollow noreferrer">Example returned XML file</a></p>
  
  <p>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.</p>
  
  <p>My code:</p>
  
  <pre><code>&lt;?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-&gt;formatOutput = true;
          $root = $dom-&gt;createElement($node_name);
          $dom-&gt;appendChild($root);
          $array2xml =
              function ($node, $array) use($dom, &amp;$array2xml)
              {
                  foreach($array as $key =&gt; $value)
                  {
                      if (is_array($value))
                      {
                          $n = $dom-&gt;createElement($key);
                          $node-&gt;appendChild($n);
                          $array2xml($n, $value);
                      }
                      else
                      {
                          $attr = $dom-&gt;createAttribute($key);
                          $attr-&gt;value = htmlspecialchars($value);
                          $node-&gt;appendChild($attr);
                      }
                  }
              };
          $array2xml($root, $array);
          return $dom-&gt;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('~[^\pL\d]+~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-&gt;data = simplexml_load_file($file);
          return $this-&gt;data;
      }
  
      public function prepare()
      {
          foreach($this-&gt;data-&gt;RESULTSET as $xml_products)
          {
              foreach($xml_products-&gt;ROW as $xml_product){
              $product = new STDClass();
              $product-&gt;sku = (string)$xml_product-&gt;COL[0]-&gt;DATA;
              $product-&gt;brand = array_map([$this, 'sanitizer'], $this-&gt;clean_explode(':', (string)$xml_product-&gt;COL[1]-&gt;DATA));
              $product-&gt;name = (string)$xml_product-&gt;COL[2]-&gt;DATA;
              $product-&gt;description = (string)$xml_product-&gt;COL[3]-&gt;DATA;
              $product-&gt;price = str_replace(array(
                  '.',
                  ','
              ) , array(
                  '',
                  '.'
              ) , (string)$xml_product-&gt;COL[4]-&gt;DATA);
              $product-&gt;weight = (string)$xml_product-&gt;COL[5]-&gt;DATA;
              $sizes_a = array_map([$this, 'sanitizer'], $this-&gt;clean_explode(':', (string)$xml_product-&gt;COL[6]-&gt;DATA));
              $sizes_b = array_map([$this, 'sanitizer'], $this-&gt;clean_explode(':', (string)$xml_product-&gt;COL[7]-&gt;DATA));
              $product-&gt;sizes = array_unique(array_merge($sizes_a, $sizes_b));
              $colors_a = array_map([$this, 'sanitizer'], $this-&gt;clean_explode("\n", (string)$xml_product-&gt;COL[8]-&gt;DATA));
              $colors_b = array_map([$this, 'sanitizer'], $this-&gt;clean_explode("\n", (string)$xml_product-&gt;COL[9]-&gt;DATA));
              $product-&gt;colors = array_unique(array_merge($colors_a, $colors_b));
              $product-&gt;tags = array_map([$this, 'sanitizer'], $this-&gt;clean_explode(',', (string)$xml_product-&gt;COL[10]-&gt;DATA));
              $product-&gt;categories = array();
              for ($i = 11; $i &lt; $xml_product-&gt;count(); $i++)
              {
                  $categories = array_map([$this, 'sanitizer'], $this-&gt;clean_explode(',', (string)$xml_product-&gt;COL[$i]-&gt;DATA));
                  $product-&gt;categories = array_unique(array_merge($product-&gt;categories, $categories));
              }
  
              if (isset($this-&gt;products[$product-&gt;name]))
              {
                  $existing_product = $this-&gt;products[$product-&gt;name];
                  $existing_product-&gt;categories = array_unique(array_merge($existing_product-&gt;categories, $product-&gt;categories));
                  $this-&gt;products[$product-&gt;name] = $existing_product;
              }
              else
              {
                  $this-&gt;products[$product-&gt;name] = $product;
              }
                  return $product;
  
              }
  
          }
  
      }
  
  
  
      public function export()
      {
          $xml_product_ids = array();
          if (count($this-&gt;products) &gt; 0)
          {
              foreach($this-&gt;products as $xml_product)
              {
                  $xml_product_ids[] = $xml_product-&gt;sku;
                  $product = !empty($products) ? $products[0] : null;
                  $update = $product != null;
                  if (!$update)
                  {
                      $product = array(
                          'post_title' =&gt; $xml_product-&gt;name,
                          'post_name' =&gt; $xml_product-&gt;name,
                          'post_content' =&gt; '',
                          'post_excerpt' =&gt; '',
                          'post_status' =&gt; 'publish',
                          'post_author' =&gt; 1,
                          'post_type' =&gt; 'product'
                      );
                      return $this-&gt;array2xml($product);
                  }
              }
          }
      }
  }
  
  $test = new Converter();
  $dump = $test-&gt;import('../data/karoshi.xml');
  $test-&gt;prepare();
  print_r($test-&gt;export());
  ?&gt;
  </code></pre>