如何将分类和产品XML文件合并

时间:2016-02-07 23:19:53

标签: php xml file foreach

这是我的Categories XML文件。类别从上到下:

<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>  

这是我的Product XML文件。只有类别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>

如何组合这两个文件并只转换一个xml文件?请帮我。感谢。

我需要最终的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>

2 个答案:

答案 0 :(得分:0)

使用DOMDocument和DOMXPath,您可以尝试以下脚本:

/* Init DOMDocuments: */                                                        #01
$categories = new DOMDocument();
$product    = new DOMDocument();

/* Load XML: */
$categories ->loadXML( $xml1, LIBXML_NOBLANKS );
$product    ->loadXML( $xml2, LIBXML_NOBLANKS );
$categories ->formatOutput = True;
$product    ->formatOutput = True;

/* Init xPath: */
$xPathCat   = new DOMXPath( $categories );
$xPathProd  = new DOMXPath( $product );

/* Search Product Category: */                                                  #02
$prodCat = $xPathProd->query( '/products/product/categories/category' );

if( $prodCat->length > 0 )                                                      #03
{
    $cat = $prodCat->item(0);

    /* Search Corresponding Category in Categories: */                          #04
    $found = $xPathCat->query
    ( "/categories/category/category[@id=\"{$cat->nodeValue}\"]" );

    if( $found->length )
    {
        foreach( $found->item(0)->parentNode->childNodes as $child )            #05
        {
            /* Append ChildNode <category>Category Name</category>: */
            $cat->parentNode->appendChild
            (
                $product->createElement( 'category', $child->getAttribute( 'name' ) )
            );
        }
        /* Remove Old Category (numeric): */                                    #06
        $cat->parentNode->removeChild( $cat );
    }
}

echo $product->saveXML().PHP_EOL;

eval.in demo

  1. 我们为类别创建一个DOMDocument,为产品创建一个$xml1;然后我们加载相应的XML(您必须使用变量名更改$xml2->load( filePath );如果要直接从文件加载它,则必须使用->loadXML( var )而不是{{1 }});最后,我们初始化两个DOMXPath以执行简单查询;

  2. 我们执行xPath查询以在产品xml中找到节点/products/product/categories/category;

  3. 如果查询成功,我们处理第一个类别项目(此脚本最终省略了其他类别项目,尽管可以使其适用于多个项目),并且

  4. 我们执行xPath查询以在类别xml(category[@id=)中找到产品类别ID;

  5. 如果查询成功,我们处理每个类别节点兄弟,检索他的属性'name',我们在新<category> product元素的节点值处使用,然后我们将此新元素追加到最后实际<category>产品元素的兄弟;

  6. foreach循环后,我们删除原始的数字<category>产品元素并打印出结果XML。

  7. 编辑:

    您的类别文件与提供的示例不同。另外,正如您在§3上看到的那样,我的脚本仅适用于第一个产品类别匹配。要正确运行文件,请以这种方式更改上面的脚本:

    (...)
    /* Search Product Category: */
    foreach( $xPathProd->query( '/products/product/categories' ) as $node )
    {
        $prodCat = $xPathProd->query( './category', $node );
        if( $prodCat->length > 0 )
        {
            $cat = $prodCat->item(0);
            $found = $xPathCat->query( "//category[@id=\"{$cat->nodeValue}\"]" );
            if( $found->length )
            {
                foreach( $found->item(0)->parentNode->childNodes as $child )
                {
                    /* Append ChildNode <category>Category Name</category>: */
                    $cat->parentNode->appendChild
                    (
                        $product->createElement( 'category', htmlspecialchars($child->getAttribute( 'name' )) )
                    );
                }
                /* Remove Old Category (numeric): */
                $cat->parentNode->removeChild( $cat );
            }
        }
    }
    

答案 1 :(得分:0)

请检查以下代码。什么是错误:

脚本链接:https://hobiall.com/xml/xml_combine.php    最终XML链接:https://hobiall.com/xml/final.xml(但产品文件相同)

<?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 and 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 as $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";
?>