Home  >  Article  >  Backend Development  >  How to modify data in php simplexml

How to modify data in php simplexml

藏色散人
藏色散人Original
2021-10-26 09:39:111980browse

php 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.

How to modify data in php simplexml

##The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

php simplexmlHow to modify data?

PHP's SimpleXML modifies XML data

1 Code

<?php
/*  设置编码格式  */
header(&#39;Content-Type:text/html;charset=utf-8&#39;);
/*  创建XML格式的字符串  */
$str = <<<XML
<?xml version=&#39;1.0&#39; encoding=&#39;gb2312&#39;?>
<object name=&#39;商品&#39;>
	<book>
		<computerbook type=&#39;PHP入门应用&#39;>PHP从入门到精通</computerbook>
</book>
</object>
XML;
/*  ***************************  */
/*  创建SimpleXML对象  */
$xml = simplexml_load_string($str);
/*  输出根目录属性name的值  */
echo $xml[name].&#39;<br />&#39;;
/*  修改子元素computerbook的属性值type  */
$xml->book->computerbook[&#39;type&#39;] = iconv(&#39;gb2312&#39;,&#39;utf-8&#39;,&#39;PHP程序员必备工具&#39;);
/*  修改子元素computerbook的值  */
$xml->book->computerbook = iconv(&#39;gb2312&#39;,&#39;utf-8&#39;,&#39;PHP函数参考大全&#39;);
/*  输出修改后的属性和元素值  */
echo $xml->book->computerbook[&#39;type&#39;].&#39; => &#39;;
echo $xml->book->computerbook;
?>

2 Running results

Product

Must-have tools for PHP programmers => PHP function reference collection

Recommended learning: "

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn