So i have the following xml structure:
<Application>
<Properties>
<Property>
<Name>blabla</Name>
<Value>123</Value>
</Property>
</Properties>
</Application>
and i want to add another “Property” child with PHP.
an example could be:
<Application>
<Properties>
<Property>
<Name>blabla</Name>
<Value>123</Value>
<Name>example test</Name>
<Value>another value</Value>
</Property>
</Properties>
</Application>
Here is my current php code:
<?php
$xml = simplexml_load_file("Application.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$properties = $sxe->addChild("Property");
$properties->addChild("Name", "namehere");
$properties->addChild("Value", "random value here");
$sxe->asXML("Application.xml");
?>
but it just adds it to the end of the xml. After </Application> and that is not what we want.
I want it to add it in the <Property> child.
Can someone help me?
You need to traverse inside the
<Application>tag to the<Properties>tag.Use
xpath().Here’s the phpfiddle