I want to add a new child node in a xml file using PHP. I already have a working function.
What I want now is, that this new child node is added as first child node in the file. For now it is added at the end of the file.
Example of existing file:
<?xml version="1.0" encoding="utf-8"?>
<News>
<NewsModel>
<ID>1</ID>
<Headline></Headline>
<ShortDescription></ShortDescription>
<Description></Description>
<LinkText/>
<Link/>
</NewsModel>
<NewsModel>
<ID>2</ID>
<Headline></Headline>
<ShortDescription></ShortDescription>
<Description></Description>
<LinkText/>
<Link/>
</NewsModel>
<NewsModel>
<ID>3</ID>
<Headline></Headline>
<ShortDescription></ShortDescription>
<Description></Description>
<LinkText></LinkText>
<Link/>
</NewsModel>
</News>
Now the new child node is added after the ID 3. I want that the new child node is added on top of ID 1.
My function:
$xml=simplexml_load_file($xmlurl)or die("Kann keine Verbindung zu $xmlurl aufbauen");
$entry = $xml->addChild('NewsModel');
$entry->addChild('ID',$_POST['inputIDNumber']);
$entry->addChild('Headline',$_POST['inputHeadline']);
$entry->addChild('ShortDescription',$_POST['inputShorDesc']);
$entry->addChild('Description',$_POST['inputDesc']);
$entry->addChild('LinkText',$_POST['inputLinkText']);
$entry->addChild('Link',$_POST['inputLink']);
$xml->addChild($entry);
file_put_contents($xmlurl, $xml->asXML(), 0, stream_context_create(['ftp' => ['overwrite' => true]]));
How is it possible, to do that?
Thanks