php - How to Categoriy and Product XML files combined -
this categories xml file. category levels top bottom :
<categories> <category> <category id="17" name="off-road" /> <category id="141" name="hpi - maverick" /> <category id="453" name="hsp" /> <category id="412" name="diger" /> </category> <category> <category id="124" name="benzinli" /> <category id="125" name="off-road" /> <category id="295" name="mcd racing" /> <category id="315" name="rtr" /> <category id="316" name="kit versiyonları" /> </category> </categories>
this product xml file. have category id:
<products> <product> <id>001300v4</id> <name>mcd rrv4 competition - no engine</name> <price>1049</price> <stock>2</stock> <currency>euro</currency> <brand>mcd racing</brand> <description> <![cdata[......]]> ]]> </description> <categories> <category>316</category> </categories> </product>
how can combined 2 files , convert 1 xml file? please me. thanks.
i need final xml :
<products> <product> <id>001300v4</id> <name>mcd rrv4 competition - no engine</name> <price>1049</price> <stock>2</stock> <currency>euro</currency> <brand>mcd racing</brand> <description><![cdata[......]]></description> <categories> <category>benzinli</category> <category>off-road</category> <category>mcd racing</category> <category>kit versiyon</category> </categories> </product> </products>
please check following code. mistake :
script link: https://hobiall.com/xml/xml_combine.php final xml link: https://hobiall.com/xml/final.xml (but same product file)
<?php $category_dosyasi_orj="https://hobiall.com/xml/categories.xml"; //kategori dosyasının xml linki $products_dosyasi_orj="https://hobiall.com/xml/products.xml"; //Ürün dosyasının xml linki $final_xml="/srv/users/serverpilot/apps/hobiall/public/xml/final.xml"; // save path , file name /* init domdocuments: */ #01 $categories = new domdocument('1.0'); $product = new domdocument('1.0'); /* load xml: */ $categories ->load( $category_dosyasi_orj, libxml_noblanks ); $product ->load( $products_dosyasi_orj, libxml_noblanks ); $categories ->formatoutput = true; $product ->formatoutput = true; /* init xpath: */ $xpathcat = new domxpath( $categories ); $xpathprod = new domxpath( $product ); /* search product category: */ $prodcat = $xpathprod->query( '/products/product/categories/category' ); if( $prodcat->length > 0 ) { $cat = $prodcat->item(0); /* search corresponding category in categories: */ $found = $xpathcat->query ( "/categories/category/category[@id=\"{$cat->nodevalue}\"]" ); if( $found->length ) { foreach( $found->item(0)->parentnode->childnodes $child ) { /* append childnode <category>category name</category>: */ $cat->parentnode->appendchild ( $product->createelement( 'category', $child->getattribute( 'name' ) ) ); } /* remove old category (numeric): */ $cat->parentnode->removechild( $cat ); } } $xml= $product->savexml().php_eol; $file=fopen($final_xml,"w") or die ("cant open"); fwrite($file, $xml); fclose($file); echo "xml file saved"; ?>
Comments
Post a Comment