1

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

BiSaM
  • 291
  • 1
  • 16
  • Does this answer your question? [PHP SimpleXML: insert node at certain position](https://stackoverflow.com/questions/3361036/php-simplexml-insert-node-at-certain-position) – CBroe Jan 22 '21 at 08:03

1 Answers1

3

Be aware that SimpleXML is a very simple implementation of a XML API in PHP.

So, there's no SimpleXML function that directly achieves what you want. But: You can use a different approach of building your XML structure:

<?php

$newXml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><News></News>');

$entry = $newXml->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 = simplexml_load_file($xmlurl) or die("Kann keine Verbindung zu $xmlurl aufbauen");

foreach ($xml as $child) {
    $entry = $newXml->addChild('NewsModel');
    $entry->addChild('ID', $child->ID);
    $entry->addChild('Headline', $child->Headline);
    $entry->addChild('ShortDescription', $child->ShortDescription);
    $entry->addChild('Description', $child->Description);
    $entry->addChild('LinkText', $child->LinkText);
    $entry->addChild('Link', $child->Link);
}
file_put_contents($xmlurl, $newXml->asXML(), 0, stream_context_create(['ftp' => ['overwrite' => true]]));

You start by creating a new XML structure and adding the new NewsModel entry to it. After that you import the existing NewsModels.

Dharman
  • 30,962
  • 25
  • 85
  • 135
René Pöpperl
  • 567
  • 5
  • 18