php - 我想在 PHP 中运行 cron 时向现有 XML 添加动态子项

标签 php xml dynamic cron

我有一个现有的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/

相关文章:

PHP-PDO 错误 : An invalid keyword 'host' was specified in the DSN string

xml - 向嵌套元素添加命名空间

asp.net - 转发器中的动态下拉列表,ASP.NET

c# - 以编程方式使用用户控件

php - 使用 PHP 将 MySQL 中的行解析到文件中不起作用

php - 插入 0000-00-00 00 :00:00 to DateTime in Mysql Database

c# - xsd.exe 为 XML 序列化生成奇怪的 C# 类

xml - XSD 定义具有属性和所需内容的元素

最大尺寸未知时的 MatLab 内存分配

php - 大型mysql数据库查询