addChild($nodename,'',$namespace); //Added a nodename to create inside the function $node1 = dom_import_simplexml($node); $no = $node1->ownerDocument; $node1->appendChild($no->createCDATASection($cdata_text)); return $node; } } header ('Content-type: text/html; charset=utf-8'); function leerFichero($archivo) { if (!$muebles_xml = simplexml_load_file($archivo)) { trigger_error('Error reading XML file',E_USER_ERROR); } $muebles = array(); $cont = 0; foreach($muebles_xml as $mueble_xml) { $muebles[$cont]['titulo'] = trim($mueble_xml->titulo); $muebles[$cont]['slug'] = preg_replace("![^a-z0-9]+!i", "-", strtoupper(trim($muebles[$cont]['titulo']))); $muebles[$cont]['precio'] = trim($mueble_xml->precio); $muebles[$cont]['categoria_name'] = $mueble_xml->categoria_name; $muebles[$cont]['categoria'] = $mueble_xml->categoria; $muebles[$cont]['descripcion'] = ''; if (strlen($mueble_xml->medidas) > 0) { $muebles[$cont]['descripcion'] .= trim($mueble_xml->medidas) . 'cm'; } if (strlen($mueble_xml->obs) > 0) { $muebles[$cont]['descripcion'] .= ' ' . trim($mueble_xml->obs); } $cont++; } return $muebles; } function escribirFicheroXML($muebles, $archivo_salida) { include "cabecera.php"; $xml = new SimpleXMLExtend($xmlstr); $raiz = $xml->channel; foreach($muebles as $mueble) { $item = $raiz->addChild('item'); $item->addChild('title', $mueble['titulo']); $item->addChild('link', 'http://localhost/originalhouse/?tcp_product='.$mueble['slug']); $item->addChild('pubDate', 'Wed, 15 Jul 2011 00:00:00 +0000'); $item->addChild('dc:creator', 'rodax'); $guid = $item->addChild('guid', 'http://localhost/originalhouse/?tcp_product&p='.$mueble['slug']); $guid->addAttribute('isPermaLink' , 'false'); $item->addChild('description', ''); $item->addCData('content:encoded', $mueble['descripcion'], 'http://purl.org/rss/1.0/modules/content/'); $item->addCData('excerpt:encoded', '', 'http://purl.org/rss/1.0/modules/excerpt/'); //227 $item->addChild('post_date', '2011-07-15 17:09:59', 'http://wordpress.org/export/1.1/'); $item->addChild('post_date_gmt', '2011-07-15 17:09:59', 'http://wordpress.org/export/1.1/'); $item->addChild('post_name', $mueble['slug'], 'http://wordpress.org/export/1.1/'); $item->addChild('status', 'publish', 'http://wordpress.org/export/1.1/'); $item->addChild('post_type', 'tcp_product', 'http://wordpress.org/export/1.1/'); $categoria = $item->addCData('category', $mueble['categoria']); $categoria->addAttribute('domain', 'tcp_product_category'); $categoria->addAttribute('nicename', $mueble['categoria_name']); $meta = $item->addChild('wp:postmeta', '', 'http://wordpress.org/export/1.1/'); $meta->addChild('wp:meta_key', 'tcp_price', 'http://wordpress.org/export/1.1/'); $meta->addCData('wp:meta_value', $mueble['precio'], 'http://wordpress.org/export/1.1/'); } return $xml->asXML($archivo_salida); } /* INSERT INTO `wp_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_status`, `comment_status`, `ping_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `guid`, `menu_order`, `post_type`, `comment_count`) VALUES (1, now(), now(), '85x130x45 cm', 'ACRO 66ML', 'publish', 'closed', 'open', 'ACRO 66ML', now(), now(), 0, 'http://localhost/originalhouse/?post_type=tcp_product&p=214', 0, 'tcp_product', 0), * insert into wp_postmeta (post_id, meta_key, meta_value) values (227, 'tcp_price', 697); */ function escribirFichero($muebles, $archivo_salida) { $file = fopen($archivo_salida, "w+"); if (!$file) { trigger_error('Error creando fichero de salida', E_USER_ERROR); } $cont = 0; foreach($muebles as $mueble) { generarRegistrosMueble($mueble, $cont, $file); $cont++; } fclose($file); } function generarRegistrosMueble($mueble, $orden, $file) { /* INSERT INTO `wp_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_status`, `comment_status`, `ping_status`, `post_name`, * `post_modified`, `post_modified_gmt`, `post_parent`, `guid`, `menu_order`, `post_type`, `comment_count`) VALUES (1, now(), now(), '85x130x45 cm', 'ACRO 66ML', 'publish', 'closed', 'open', 'ACRO 66ML', now(), now(), 0, 'http://localhost/originalhouse/?post_type=tcp_product&p=214', 0, 'tcp_product', 0), * insert into wp_postmeta (post_id, meta_key, meta_value) values (227, 'tcp_price', 697); */ $output = "INSERT INTO `wp_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_status`, `comment_status`, `ping_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `guid`, `menu_order`, `post_type`, `comment_count`) VALUES \n"; $output .= "("; $output .= "1, now(), now(), "; $output .= "'" . $mueble['descripcion'] . "', "; // post_content $output .= "'" . $mueble['titulo'] . "', "; // post_title $output .= "'publish', 'closed', 'open', "; $output .= "'" . $mueble['slug'] . "', "; // post_name $output .= "now(), now(), 0, "; $output .= "'http://localhost/originalhouse/?tcp_product&p=".$mueble['slug']."', "; $output .= "0, 'tcp_product', 0"; $output .= ");"; $output .= "\n"; fwrite($file, $output); // $output = "SET @last_id := LAST_INSERT_ID();\n"; fwrite($file, $output); //INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES $output = "INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES \n"; $output .= "(@last_id, 51, 0);\n"; fwrite($file, $output); // metadatos $output = "insert into wp_postmeta (post_id, meta_key, meta_value) values \n"; // precio $output .= "(@last_id, 'tcp_price', '" . $mueble['precio'] . "'),\n"; //stock $output .= "(@last_id, 'tcp_stock', -1),\n"; //tipo $output .= "(@last_id, 'tcp_type', 'SIMPLE'),\n"; //order $output .= "(@last_id, 'tcp_order', '". $orden . "'),\n"; //sku $output .= "(@last_id, 'tcp_sku', '".$mueble['slug']."'),\n"; $output .= "(@last_id, 'tcp_tax_id', '0'),\n"; $output .= "(@last_id, 'tcp_back_end_label', ''),\n"; $output .= "(@last_id, 'tcp_hide_buy_button', ''),\n"; $output .= "(@last_id, 'tcp_exclude_range', ''),\n"; $output .= "(@last_id, 'tcp_is_downloadable', ''),\n"; $output .= "(@last_id, 'tcp_max_downloads', '0'),\n"; $output .= "(@last_id, 'tcp_days_to_expire', '0'),\n"; $output .= "(@last_id, 'tcp_is_visible', '1'),\n"; $output .= "(@last_id, 'tcp_weight', '0');\n"; fwrite($file, $output); //SELECT * FROM `wp_posts` WHERE `post_type` = 'attachment' and `post_mime_type` = 'image/jpeg' and `post_name` = $mueble['slug'] //imagen $output = "insert into wp_postmeta (post_id, meta_key, meta_value) \n"; $output .= "SELECT @last_id, '_thumbnail_id', wp_posts.id FROM `wp_posts` WHERE `post_type` = 'attachment' and `post_mime_type` = 'image/jpeg' and "; $output .= "`post_title` = '" . $mueble['slug'] . "';\n\n\n"; fwrite($file, $output); } /* * ACRO 66ML http://localhost/originalhouse/?tcp_product=acro-66ml Wed, 06 Jul 2011 17:09:59 +0000 rodax http://localhost/originalhouse/?post_type=tcp_product&p=214 227 2011-07-06 17:09:59 2011-07-06 17:09:59 closed closed acro-66ml publish 0 0 tcp_product 0 tcp_price * */ $archivo = 'sofas.xml'; $archivo_salida = 'salida-sofas.sql'; $muebles = leerFichero($archivo); //var_dump($muebles); echo escribirFichero($muebles, $archivo_salida); /*if ($muebles_xml) { foreach($muebles_xml as $mueble_xml) { $muebles['id'] = $mueble_xml->Id; $muebles['titulo'] = $mueble_xml->titulo; $muebles['slug'] = preg_replace("![^a-z0-9]+!i", "-", strtolower($muebles[titulo])); print_r($muebles); }*/ /*foreach($muebles->children() as $child) { echo $child->getName().": "; foreach($child->children() as $subchild) { echo "--->".$subchild->getName().": ".$subchild." "; } }*/ ?>