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 andFALSE
on error. If the parameter is specified, it
returnsTRUE
if the file was written successfully andFALSE
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
Get the server object/array where $serverName = "Google"
// An array of all <server> elements with the chosen name
$googles = $servers->xpath('server[name = " Google "]');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';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
Parse Math Operations with PHP
Storing Time Information: Timezone Required
Populate Select Drop Down from a Database Table
PHP Include File in Webroot from File Outside Webroot
Printing Content of a Xml File Using Xml Dom
How to Enable Put Requests in Azure
Multi Checkbox Fields in Woocommerce Backend
Select MySQL Rows But Rows into Columns and Column into Rows
How to Get Value from Form Without Submitting It
PHP Using Declare? What Is a Tick
Linux - PHP 7.0 and Mssql (Microsoft SQL)
PHP - How to Send an Array to Another Page
PHP Not Loading PHP_Pgsql.Dll on Windows
Nginx Issues Http 499 Error After 60 Seconds Despite Config. (PHP and Aws)
How to Generate a Custom Menu/Sub-Menu System Using Wp_Get_Nav_Menu_Items in Wordpress