XML:为每个产品添加子项 - 不工作(PHP)

时间:2017-01-05 00:25:14

标签: php xml

我希望通过从标题或网址获取平台来为平台添加平台。但问题是我保存平台的阵列没有产品列表那么大。但那不可能! 因为在每种情况下我都会将一个值推入数组......所以它应该是大的!

但它并不是......代码也没有将Child添加到XML中。但我不知道为什么......我怎么能解决这个问题?

代码:

function isContaining($searches, $titleTag, $urlTag, $productTag, $path){

$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($path);
$root = $dom->documentElement;

$markerTitle = $root->getElementsByTagName($titleTag);
$markerURL = $root->getElementsByTagName($urlTag);

$plat = array();

for($i = $markerTitle->length - 1; $i >= 0 ; $i--){

    $title = $markerTitle->item($i)->textContent;
    $url = $markerURL->item($i)->textContent;

    $size = count($searches);

    for($j = 0; $j < $size; $j++){
        if(stripos($title, $searches[$j])){
            if($j > 4){
                array_push($plat, "PlayStation");
            }elseif($j < 5){
                array_push($plat, $searches[$j]);
            }else{
                array_push($plat, "Nothing");
            }
        }elseif(stripos($url, $searches[$j])){
            if($j > 4){
                array_push($plat, "PlayStation");
            }elseif($j < 5){
                array_push($plat, $searches[$j]);
            }else{
                array_push($plat, "Nothing");
            }
        }
    }
}

array_reverse($plat);
print_r($plat);
$count = 0;

foreach($root->getElementsByTagName($productTag) as $product){
    $newElement = $dom->createElement('plat', $plat[$count]);
    $product->appendChild($newElement);
    $count = $count + 1;
    }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<products>
  <product>
    <name>Final Fantasy VII Steam</name>
    <desc>Den Rollenspiel-Klassiker FINAL FANTASY VII gibt es jetzt für den PC, mit brandneuen Online-Features!</desc>
    <price>11.69</price>
    <price_base>12.99</price_base>
    <link>https://de.gamesplanet.com/game/final-fantasy-vii-download--1001-1</link>
    <publisher>Eidos - Square Enix</publisher>
    <category>RPG (Rollenspiel)</category>
    <ean/>
    <packshot>https://de.gamesplanet.com/acache/10/01/1/de/packshot-770bf6d03800b87dc0f9509f21e8d423.jpg</packshot>
  </product>

问候

0 个答案:

没有答案