Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

xml - Add new child node with php simplexml on top of file

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

question from:https://stackoverflow.com/questions/65840286/add-new-child-node-with-php-simplexml-on-top-of-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...