How to update this xml file with PHP XML reader & writer?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 20:00:18

XMLWriter is not good for this methodology. You should use a different library, like simplexml for instance.

With that, it's very simple. Although I don't know what your doc structure looks like, let's take a stab at it:

//load the file for our manipulating
$xml = simplexml_load_file($file);

//grab the parent element that we want to append to
$urls = $xml->urls;

//add a new child called Url
$newUrl = $urls->addChild('url');

//add a new child called loc to the new child Url we just created, add a link to yahoo
$newUrl->addChild('loc', 'http://www.yahoo.com');

//write the output
$xml->asXML($xml);

Here's an eval.in example

I waiting for a good response, I have the some problem (xml file with over 50.000 elements) and I would like to add element without load full xml in memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!