0

我对 wp all import 有疑问...我尝试解释一下:

这是我的 XML

<Node>
<CodeID>1533</CodeID>
....
<Size>36|37|38</Size>
</Node>

问题是我需要一个 Size 的子元素

<Variations>
     <Variation1>36</Variation1>
     <Variation2>37</Variation2>
     <Variation3>38</Variation3>
    </Variations>

有没有一种方法或函数可以修改为拥有这个 xml 并将所有变体添加为子元素,而不是全部在同一行中?

4

1 回答 1

0
<?php


$string = "<?xml version='1.0'?> 
<Item>
<Node>
<CodeID>1533</CodeID>
<Size>36|37|38</Size>
</Node>
<Node>
<CodeID>L534</CodeID>
<Size>S|M|L</Size>
</Node>
</Item>";

$xml = simplexml_load_string($string);
$xml2 = simplexml_load_string($string);
//$xml = simplexml_load_file("./Items.xml", 'SimpleXMLElement', LIBXML_NOCDATA);
$i=0;
foreach ($xml as $item)
{
    $variations_array = explode("|",$item->Size);

    $j=0;
    foreach ($variations_array as $variation)
    {
        $xml2->Node[$i]->Variations->Variation[$j] = $variation;
        $j++;
    }
$i++;   
}
echo $xml2->asXML();
//$xml2->asXML("./Items2.xml");

/*
<?xml version="1.0"?>
<Item>
<Node>
<CodeID>1533</CodeID>
<Size>36|37|38</Size>
<Variations>
<Variation>36</Variation>
<Variation>37</Variation>
<Variation>38</Variation>
</Variations>
</Node>
<Node>
<CodeID>L534</CodeID>
<Size>S|M|L</Size>
<Variations>
<Variation>S</Variation>
<Variation>M</Variation>
<Variation>L</Variation>
</Variations>
</Node>
</Item>
*/

?>
于 2017-12-15T18:29:17.563 回答