這段代碼:
$product = $dom->createElement('o');
$product->setAttribute('id', $productSku);
$product->setAttribute('url', $productWebsite.$productUrl);
$product->setAttribute('price', substr_replace($productPrice,'.',-2,0).$productCurrency);
$product->setAttribute('avail', $productAvail);
$product->setAttribute('weight', $productWeight);
$product->setAttribute('stock', $productStock);
$product->setAttribute('basket', $productBasket);
為我生成:
<offers><o id="12264" url="http" price="209.22" avail="1" weight="1" stock="183" basket="1">
現在我添加:
//tag
$title = $dom->createElement('name', clean($productTitle));
$product->appendChild($title);
在這個 xml 行下我有
<name>some name</name>
一切正常!但我需要在所有行下添加下一個標簽才能獲得結果:
<imgs>
<main url="https://firstimage1"/>
</imgs>
所以現在我嘗試:
$product = $dom->createElement('imgs');
$product->setAttribute('main', $productImagePath.$productImage);
什么是問題?
此代碼將第一個標簽替換
o為imgs并在行尾將標簽main添加到 <o 一些像這樣的第一個標簽:<offers><imgs id="12264" url="http" price="209.22" avail="1" weight="1" stock="183" basket="1" main="https:urlphoto">
這是錯誤的。我需要充分發揮作用:
<offers><o id="12264" url="http" price="209.22" avail="1" weight="1" stock="183" basket="1">
<name>some name</name>
<imgs>
<main url="https://firstimage1"/>
</imgs>
</o>
</offers>
完整代碼:
$product = $dom->createElement('o');
$product->setAttribute('id', $productSku);
$product->setAttribute('url', $productWebsite.$productUrl);
$product->setAttribute('price', substr_replace($productPrice,'.',-2,0).$productCurrency);
$product->setAttribute('avail', $productAvail);
$product->setAttribute('weight', $productWeight);
$product->setAttribute('stock', $productStock);
$product->setAttribute('basket', $productBasket);
//tag
$title = $dom->createElement('name', clean($productTitle));
$product->appendChild($title);
// next element (issue here) this replace first tags
$product = $dom->createElement('imgs');
$product->setAttribute('main', $productImagePath.$productImage);
$root->appendChild($product);
}
$dom->appendChild($root);
$dom->save($filePath);
}
@update:我嘗試改變:
// next element
// next element
$product2 = $dom->createElement('imgs');
$product3 = $dom->createElement('main');
$product3->setAttribute('url', $productImagePath.$productImage);
并在檔案末尾:
$root->appendChild($product);
$root->appendChild($product2);
$root->appendChild($product3);
但現在我有:
<offers>
<o id="12264" url="http" price="209.22" avail="1" weight="1" stock="183" basket="1">
<name>4711 Eau De Cologne 800ml</name>
</o>
<imgs/>
@update 2:@謝謝你的回答。這個作業正確!我能問你最后一件事嗎?
我需要再次添加:
<attrs>
<a name="Producent">
<![CDATA[Avery]]>
</a>
<a name="EAN">
<![CDATA[9084692100225]]>
</a>
<a name="Kod producenta">
<![CDATA[AVSG710022]]>
</a>
</attrs>
為此,我復制了函式:
// create append "attrs" to "o"
$offer->appendChild(
$attrs = $document->createElement('attrs')
);
// create append "a" to "attrs"
$attrs->appendChild(
$attr = $document->createElement('a')
);
// add properties to the "attrs" element
$attr->setAttribute('producent', $productBrand);
$attr->setAttribute('ean', $productEan);
$attr->setAttribute('kod_producent', $productSku);
但目前我有:
<attrs>
<a producent="4711" ean="4011700740031" kod_producent="12264"/>
</attrs>
uj5u.com熱心網友回復:
您將imgs元素節點命名為變數$product并覆寫o元素 - 它的父節點。確保為變數使用語意和唯一名稱。
使用 DOM 檔案上的方法來創建節點并將它們附加到它們的父節點。appendChild()回傳附加的節點,所以你可以用它來鏈接呼叫,只需要將節點存盤到一個變數中以進行擴展修改。
不要使用 的第二個引數createElement()。它不會完全轉義特殊字符。設定textContent屬性或追加文本節點。
您可以在創建元素節點后直接追加 - 這里無需等待。您可以隨時修改附加節點 - 這與您用來操作現有節點的方法相同。
下面是創建目標 XML 結構(具有減少的屬性)的示例。
$document = new DOMDocument('1.0', 'UTF-8');
// create append the document element "offers"
$document->appendChild(
$offers = $document->createElement('offers')
);
// create append the "o" element
$offers->appendChild(
$offer = $document->createElement('o')
);
// add properties to the "o" element
$offer->setAttribute('id', 'SKU123');
$offer->setAttribute('url', 'https://example.com#product');
// create append the "name" to "o"
$offer
->appendChild($document->createElement('name'))
// chain call and set the text content
->textContent = 'Example product name';
// create append "imgs" to "o"
$offer->appendChild(
$images = $document->createElement('imgs')
);
// create append "main" to "imgs"
$images->appendChild(
$image = $document->createElement('main')
);
// add properties to the "main" element
$image->setAttribute('url', 'https://example.com#product-image');
$document->formatOutput = true;
echo $document->saveXML();
輸出:
<?xml version="1.0" encoding="UTF-8"?>
<offers>
<o id="SKU123" url="https://example.com#product">
<name>Example product name</name>
<imgs>
<main url="https://example.com#product-image"/>
</imgs>
</o>
</offers>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/535882.html
標籤:PHPXML
上一篇:批量更新spatie/ActivityLogLaravel更好的解決方案?
下一篇:將檔案的內容傳輸到集合
