How to modify data in php simplexml

藏色散人
Release: 2023-03-13 13:44:02
Original
1987 people have browsed it

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;
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template