How to Save Changed Simplexml Object Back to File

How to save changed SimpleXML object back to file?

Not sure I understand the issue. The asXML() method accepts an optional filename as param that will save the current structure as XML to a file. So once you have updated your XML with the hints, just save it back to file.

// Load XML with SimpleXml from string
$root = simplexml_load_string('<root><a>foo</a></root>');
// Modify a node
$root->a = 'bar';
// Saving the whole modified XML to a new filename
$root->asXml('updated.xml');
// Save only the modified node
$root->a->asXml('only-a.xml');

PHP: How do I write XML object into a XML file

simplexml_load_file returns a SimpleXMLElement object. If you look through the documentation you will find this:

public mixed asXML ([ string $filename ] )

In other words:

$file = "country_request.xml";
$xml = simplexml_load_file($file);

// Do you stuff here...

if ($xml->asXML($file)) {
echo 'Saved!';
} else {
echo 'Unable to save to file :(';
}

SimpleXMLElement Object back to XML

$sxml is a simpleXMLElement

$doc = new DOMDocument();
$doc->formatOutput = TRUE;
$doc->loadXML($sxml->asXML());
$xml = $doc->saveXML();

Updating a xml file with php simplexml tool

You need to save modified xml back into file

<?php
$completeurl = $_SERVER['DOCUMENT_ROOT'].'/ytplayer/videolar.xml';
$xml = simplexml_load_file($completeurl);

$xml->track[0]->title = 'Whitesnake - Is this love';
$xml->track[0]->youtube = 'GOJk0HW_hJw';

$xml->asXML($completeurl)
?>

trying to edit xml file using php simplexml

Second one is not possible as SimpleXMLElement can only take a well-formed XML string or the path or URL to an XML document. But you are passing an object of class SimpleXMLElement returned by simplexml_load_file. That is the reason it was throwing error String couldn't be parsed to XML...

In first one the asXML() method accepts an optional filename as parameter that will save the current structure as XML to a file.

If the filename isn't specified, this function returns a string on
success and FALSE on error. If the parameter is specified, it
returns TRUE if the file was written successfully and FALSE
otherwise.

So once you have updated your XML with the hints, just save it back to file.

$settings = simplexml_load_file("settings.xml");
....
if(isset($aInformation['cName']))
{
$settings->general->communityname = $aInformation['cName'];
// Saving the whole modified XML to a new filename
$settings->asXml('updated_settings.xml');
// Save only the modified node
$settings->general->communityname->asXml('settings.xml');
}

Editing XML Nodes through SimpleXML PHP

  1. Get the server object/array where $serverName = "Google"

    // An array of all <server> elements with the chosen name
    $googles = $servers->xpath('server[name = " Google "]');
  2. Edit the server's address field to something different like http://www.google.co.uk

    //Find a google and change its address
    $google->address = 'http://www.google.co.uk';
  3. Write the changes back to the XML file.

    $servers->saveXML('path/to/file.xml');

Full example

$servers = simplexml_load_file('path/to/file.xml');
$googles = $servers->xpath('server[name=" Google "]');
foreach ($googles as $google) {
$google->address = 'http://www.google.co.uk';
}
$servers->saveXML('path/to/file.xml');

More info

  • SimpleXML Basic Usage
  • XPath tutorial

edit XML with simpleXML

Sure you can edit with SimpleXML:

$input = <<<END
<?xml version='1.0' standalone='yes'?>
<documents>
<document>
<name>spec.doc</name>
</document>
</documents>
END;

$xml = new SimpleXMLElement($input);
$xml->document[0]->name = 'spec.pdf';
$output = $xml->asXML();

Take a look at the examples.



Related Topics



Leave a reply



Submit