I get the data from a file xml
as follows:
$fileXML = file_get_contents('Documents/file.xml');
$xmlfile = loadXML($fileXML);
In order not to make the question extensive, it already goes through other processes to have this:
<factura>
<infofactura>data...</infofactura>
<detalles>data...</detalles>
</factura>
To this:
<factura><infofactura>data...</infofactura><detalles>data...</detalles></factura>
Now I have information in some concatenated variables:
$tagsXML = '';
$tagsXML .= '<otraetiquetas>'.$data.'</otraaetiquetas>';
I want to insert this information from this variable $tagsXML
into the variable $xmlfile
to have the following result:
<factura><infofactura>data...</infofactura><detalles>data...</detalles><otraetiquetas>Más datos</otraetiquetas></factura>
In short, have the union of both in a variable $Docxml
and then save said information in a .xml
.
I have seen the following str_replace
that allows replacing content but I don't understand how to add this information $tagsXML
inside the tag <factura>
and the same information that should go at the end of the closing </factura>
as I have exposed in the examples of the desired result.
You can solve it in the following way:
createElement()
getElementsByTagName()
appendChild()
saveXML()
for this case.Full example
VER DEMO
:Departure: