繁体   English   中英

我想在cron的时间在PHP中运行时将动态子级添加到现有XML

[英]I want to add dynamic child to an existing XML when the time of cron runs in PHP

我有一个现有的xml文件,我想在cron在php中运行时动态添加另一个项目。 请帮我。

<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');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM