Php change xml attribute value
Basics I suggest you select all
The above statement will select all Now it is simple to iterate over
Note that the changes applied to
will show the changes. see it working: https://eval.in/499427 Expanded Of course, you might want to select only certain
Note the condition set in Today we will learn , how to edit the xml element attributes in PHP. First of all, we need to create a sample xml file. So we have created the demo.xml file as follows /** * Webkul Software. * * @category Webkul * @package xml * @author Webkul * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ After creating the xml file, we have created the php file as follows: /** * Webkul Software. * * @category Webkul * @package api * @author Webkul * @copyright Copyright (c) 2010-2017 Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ "); print_r($xml); print_r(""); if ($xml->xpath('//appSettings/add[@key="Id"]')[0]) { $xml->xpath('//appSettings/add[@key="Id"]')[0]->attributes()['value'] = 1; } if ($xml->xpath('//appSettings/add[@key="Name"]')[0]) { $xml->xpath('//appSettings/add[@key="Name"]')[0]->attributes()['value'] = 'Demo'; } $xml->asXML('demo.xml'); ?> Function Description: simplexml_load_file : The function is used to convert the xml file into SimpleXMLElement object. In this function first parameter is file path where we pass the path of our xml file. Syntax for the function is : Start your headless eCommerce SimpleXMLElement simplexml_load_file ( string $filename string $class_name , int $options , string $ns , bool is_prefix) xpath : The function searches the XML element for children matching the XPath path. Like in the example it will search the add element with in the appsetting that has an attribute key and their value is Id or Name. asXML : The function is just reverse of simplexml_load_file and converts the xml object to the xml format. When we execute the php file for the first time it will produce the following output. We can see the changes in the demo.xml file or we can execute the php file again, we will see the changed values in the xml file as follows : (PHP 5, PHP 7, PHP 8) SimpleXMLElement::attributes — Identifies an element's attributes Descriptionpublic SimpleXMLElement::attributes(?string
ParametersnamespaceOrPrefix An optional namespace for the retrieved attributes isPrefix Default to Return ValuesReturns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag. Returns ExamplesExample #1 Interpret an XML string
The above example will output: Xeoncross ¶ 12 years ago
text] => "hello world" chris at chlab dot ch ¶ 10 years ago
var_dump((int) $sxml->Table[0]['ExpandedColumnCount']); sarlak ¶ 11 years ago
totalwipeout at gmail dot com ¶ 9 years ago
$atts_array = $atts_array['@attributes'];var_dump($atts_object); //- outputs object(SimpleXMLElement)[19] jurchiks101 at gmail dot com ¶ 4 years ago
webfelipemaia at gmail dot com ¶ 9 years ago
= simplexml_load_file("file.xml"); leap ¶ 11 years ago
alan at performantsystems dot com ¶ 12 years ago
zevnikrok ¶ 7 years ago
skerr at mojavi dot org ¶ 17 years ago
= simplexml_load_file('file.xml');
adrian at foeder dot de ¶ 9 years ago
inge at elektronaut dot no ¶ 18 years ago
|