You will need to request the required tag <events>instead of the first (to return $xml->events), using xpathan XML document to request:
PHP script:
<?php
$xml_str = file_get_contents('xmlfile');
$xml = new SimpleXMLElement($xml_str);
$wantedEventsTag = $xml->xpath('/xml/events[@date="02-09-2010"]');
$wantedEventsTag = $wantedEventsTag [0];
$wantedEventsTag['attrname']='attrval';
$event = $wantedEventsTag->addChild('event');
$event->addChild('title', 'More Parser Stories');
$event->addChild('description', 'This is all about the people who make it work.');
file_put_contents('xmlfile.xml', $xml->asXML());
Example XML file with multiple tags <events>:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<settings>
<title>Calendar2</title>
<subTitle>Calendar2</subTitle>
</settings>
<events date="01-01-1999">
</events>
<events>
</events>
<events date="02-09-2010">
<event>
<title>HTML Tags</title>
<description>HTML Tags</description>
</event>
<event>
<title>Another Title</title>
<description>Another description</description>
</event>
</events>
</xml>
the script xpathwill match the required node, which we later use and add event subdomains.
source
share