首页 > 解决方案 > 使用php在xml中创建一个具有相同名称的多个输入

问题描述

我有一个 XML 文件,其中一个孩子有两个类别,但名称相同。我想为每个标题添加一个标题。我们如何在 PHP 中做到这一点?

这是我的 XML

<root>
    <result>
        <node>
            <title> Some Title Name</title>
                <categories>
                    <category> categor_one </category>
                    <category> categor_two </category>
                </categories>
        </node>

        <node>
            <title> Some Title Name</title>
            <categories>
                <category> categor_one </category>
                <category> categor_tree </category>
            </categories>
        </node>
    </result>
</root>

但我想获得这个

<root>
    <result>
        <node>
            <title> Some Title Name</title>
            <category>categor_one///categor_two </category>
            <category1>categor_one///categor_tree</category1>
        </node>
    </result>
</root>

我设法实现了一个只能正确获取的函数category,但如果标题相同,它就不起作用,因为它只是创建一个新的。

function solve_something($xml, $destination)
{
    $xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);
    $json = json_encode($xml);
    $items = json_decode($json, TRUE);
    $products = array();
    $product_data = array();

    foreach($items['result']['node'] as $item){
        $product_data['title'] = $item['title'];
        foreach ($item['categories'] as $category) {
            if (is_array($category)) {
                $product_data['category'] = implode('///', $category);
            } else {
                $product_data['category'] = $category;
            }
        }
        $products[] = $product_data;
        unset($product_data);
    }
    $path = createXML($products, $destination);
    return $path;

}

function createXML($data, $destination)
{

    $xmlDoc = new DOMDocument('1.0', 'UTF-8');
    $root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
    foreach ($data as $key => $product) {

        $productA = $root->appendChild($xmlDoc->createElement('product'));
        foreach ($product as $key1 => $val) {
            if (!empty($val)) {
                if ($key1 == 'price' || $key1 == 'tax' || $key1 == 'stockAmount') {
                    $productA->appendChild($xmlDoc->createElement($key1, $val));
                } else {
                    $ProductKey = $productA->appendChild($xmlDoc->createElement($key1));
                    $ProductKey->appendChild($xmlDoc->createCDATASection($val));
                }
            }
        }
    }
    $xmlDoc->formatOutput = true;
    fn_rm($destination);
    $xmlDoc->save($destination);
    return $destination;
}

我的代码的输出是这样的

    <root>
        <product>
            <title> Some Title Name</title>
            <category>categor_one///categor_two </category>
        </product>
        <product>
            <title> Some Title Name</title>
                <category>categor_one///categor_tree</category>
         </product>
     </root>

标签: phpxmlimplodecreateelement

解决方案


如果您想将数据与相同的标题保持在一起,一种方法可能是通过使用title作为数组键(如果它是有效的数组键)预先收集该数据

创建 xml 时,您将外部 foreach 循环中的标题作为键,而在内部 foreach 中,您可以使用 implode 创建元素。

请注意,在您开始使用的代码中,product因此我将其作为节点名称。

示例代码,您可以在代码中使用:

$products = array();
$product_data = array();
$xml = simplexml_load_file($xml, "SimpleXMLElement", LIBXML_NOCDATA);

foreach ($xml->result->node as $node) {
    $product_data['title'] = (string)$node->title;
    foreach($node->categories as $category) {
        if (is_array($category)) {
            $product_data['category'] = implode('///', $category);
            continue;
        } 
        $product_data['category'] = (array)$category->category;        
    }
    $products[(string)$node->title][] = $product_data;
}

$xmlDoc = new DOMDocument('1.0', 'UTF-8');
$root = $xmlDoc->appendChild($xmlDoc->createElement("root"));
foreach ($products as $key => $product) {
    $productA = $root->appendChild($xmlDoc->createElement('product'));
    $productA->appendChild($xmlDoc->createElement("title", $key));
    for ($i = 0; $i < count($product); $i++) {        
        $productA->appendChild($xmlDoc->createElement("category" . $i, implode("///", $product[$i]["category"])));
    }
}
$xmlDoc->formatOutput = true;
echo $xmlDoc->saveXML();

输出

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <product>
    <title> Some Title Name</title>
    <category0> categor_one /// categor_two </category0>
    <category1> categor_one /// categor_tree </category1>
  </product>
</root>

php演示


推荐阅读