How to append to a XML file with PHP preferably with SimpleXML

后端 未结 1 1414
轻奢々
轻奢々 2020-12-01 17:39

I have a XML file which looks like this:



    
    

    

        
相关标签:
1条回答
  • 2020-12-01 17:53

    With SimpleXML, you can use the addChild() method.

    $file = 'xml/config.xml';
    
    $xml = simplexml_load_file($file);
    
    $galleries = $xml->galleries;
    
    $gallery = $galleries->addChild('gallery');
    $gallery->addChild('name', 'a gallery');
    $gallery->addChild('filepath', 'path/to/gallery');
    $gallery->addChild('thumb', 'mythumb.jpg');
    
    $xml->asXML($file);
    

    Be aware that SimpleXML will not "format" the XML for you, however going from an unformatted SimpleXML representation to neatly indented XML is not a complicated step and is covered in lots of questions here.

    0 讨论(0)
提交回复
热议问题