我有一个现有的xml文件,我想在php中运行cron时动态添加另一个项目。请帮助我。
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<item>
<title>Diamond Cross Ring White Gold Rose Gold Yellow Gold Index Finger Ring</title>
<link>link-to-product-details-page</link>
<guid>product-id</guid>
<description>product-description</description>
<img>product-image</img>
<pubDate>product-creation-date</pubDate>
<price>price</price>
</item>
<item>
<title>Diamond Cross Ring White Gold Rose Gold Yellow Gold Index Finger Ring</title>
<link>link-to-product-details-page</link>
<guid>product-id</guid>
<description>product-description</description>
<img>product-image</img>
<pubDate>product-creation-date</pubDate>
<price>price</price>
</item>
<item>
<title>Diamond Cross Ring White Gold Rose Gold Yellow Gold Index Finger Ring</title>
<link>link-to-product-details-page</link>
<guid>product-id</guid>
<description>product-description</description>
<img>product-image</img>
<pubDate>product-creation-date</pubDate>
<price>price</price>
</item>
</rss>
最佳答案
Hi here is the solution for your problem, by using the DOM we can maipilate our XML data. Check below example
Create abject for DOM
$dom = new DOMDocument();
$dom->formatOutput = true;
//Here we can load existing xml file.
$dom->load('path-to-your-xml-file', LIBXML_NOBLANKS);
//Appneding child to root
$root = $dom->documentElement;
$newresult = $root->appendChild( $dom->createElement('item') );
$newresult->appendChild( $dom->createElement('title',dynamic-product-title) );
$newresult->appendChild( $dom->createElement('link',dynamic-link) );
$newresult->appendChild( $dom->createElement('guid',dynamic-product-id) );
$newresult->appendChild( $dom->createElement('description',dynamic-product-
desciption) );
$newresult->appendChild( $dom->createElement('img',dynamic-product-image) );
$newresult->appendChild( $dom->createElement('pubDate',dynamic-date) );
$newresult->appendChild( $dom->createElement('price',dynamic-price) );
//Here we can save our data into existing xml file.
$dom->save('path-to-your-xml-file') or die('XML Manipulate Error');
关于php - 我想在 PHP 中运行 cron 时向现有 XML 添加动态子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59081096/