##The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computerphp simplexml method to modify data: 1. Create a PHP sample file and set the encoding format; 2. Create a string in XML format; 3. Create a SimpleXML object through "simplexml_load_string($str);"; 4. Just modify the element value.
php simplexmlHow to modify data?
PHP's SimpleXML modifies XML data1 Code<?php /* 设置编码格式 */ header('Content-Type:text/html;charset=utf-8'); /* 创建XML格式的字符串 */ $str = <<<XML <?xml version='1.0' encoding='gb2312'?> <object name='商品'> <book> <computerbook type='PHP入门应用'>PHP从入门到精通</computerbook> </book> </object> XML; /* *************************** */ /* 创建SimpleXML对象 */ $xml = simplexml_load_string($str); /* 输出根目录属性name的值 */ echo $xml[name].'<br />'; /* 修改子元素computerbook的属性值type */ $xml->book->computerbook['type'] = iconv('gb2312','utf-8','PHP程序员必备工具'); /* 修改子元素computerbook的值 */ $xml->book->computerbook = iconv('gb2312','utf-8','PHP函数参考大全'); /* 输出修改后的属性和元素值 */ echo $xml->book->computerbook['type'].' => '; echo $xml->book->computerbook; ?>
Must-have tools for PHP programmers => PHP function reference collection
PHP Video Tutorial"
The above is the detailed content of How to modify data in php simplexml. For more information, please follow other related articles on the PHP Chinese website!