【问题标题】:How to Categoriy and Product XML files combined如何合并类别和产品 XML 文件
【发布时间】:2016-02-07 23:19:53
【问题描述】:

这是我的类别 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>  

这是我的产品 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>

【问题讨论】:

  • 在我们最终的 xml 中你放了 4 个 标签,有什么标准?在“产品 XML”中,我只能看到 1 个 id 为 316 的类别,所以我希望只在最终的 xml 中插入 &lt;category&gt;Kit Versiyonları&lt;/category&gt; (id=316)。你能解释一下入选标准吗?
  • 另外,要么你的xml无效。可以仔细检查并重新编辑帖子吗?
  • 顶级类别 和子类别 等...
  • 每个原产地的产品只有一个类别?
  • 是的,每个产品只有一个类别。

标签: php xml file foreach


【解决方案1】:

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

脚本链接: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";
?>

【讨论】:

    【解决方案2】:

    使用 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,为产品创建一个;然后我们加载相应的 XML(你必须用你的变量名更改 $xml1$xml2;如果你想直接从文件中加载它,你必须使用 -&gt;load( filePath ) 而不是 -&gt;loadXML( var ));最后,我们初始化两个DOMXPath 来执行简单的查询;

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

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

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

    5. 1234563实际&lt;category&gt; 产品元素;
    6. foreach 循环之后,我们删除原始的数字&lt;category&gt; 产品元素并打印出生成的XML。



    编辑:

    您的 categories 文件与提供的示例不同。此外,正如您在上面的§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 );
            }
        }
    }
    

    【讨论】:

    • 非常感谢。我现在就试试。
    • 如何修复以下错误:警告:DOMDocument::loadXML(): Start tag expected, '<'在实体中找不到,第 1 行
    • 警告:DOMDocument::loadXML():需要开始标签,在实体中找不到'
    • 请联系我以获得付费支持:bulentustun33 [at] hotmail
    • 好的,我使用了 lod (filePath) 并保存了 xml,但不包含类别名称。 :(
    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多