I have a xml file named test.xml with the following structure.
<?xml version="1.0"?>
<markers>
<marker id="1" name="12" signal="23"/>
<marker id="2" name="12" signal="5"/>
<marker id="3" name="12" signal="6"/>
<marker id="4" name="12" signal="0"/>
</markers>
I make an ajax request to my server code where I sent an ID
and want to increase the signal
attribute in test.xml
by 1.
For example if I send id=3
then the signal
attribute of the marker
node with id
of 3
should get 7
and the file should be saved.
I don't want to rewrite the whole file due to some issues but only update an attribute.
I tried this but I think it's far away from the right answer. Can anyone help?
$id=$_POST['id'];
$xml = new SimpleXMLElement('test.xml');
foreach( $xml->xpath( 'marker' ) as $xml_row ){
if( $xml_row->attributes( 'id' ) == $id ){
$xml_row->attr( 'signals ') = $xml_row->attributes( 'signals' ) + 1;
}
}
$xml->saveXML( 'test.xml' );