Home  >  Article  >  Backend Development  >  PHP怎么写XML文件

PHP怎么写XML文件

WBOY
WBOYOriginal
2016-06-13 13:18:38792browse

PHP如何写XML文件?
已有一个XML文件,代码如下:

XML code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?xml version="1.0" encoding="GB2312"?>
<appconfig>
  <com>COM1</com>
  <phone>10086</phone>
  <content>测试</content>
</appconfig>


有一个PHP变量$phone,希望将xml文件中"Phone"标签中的内容换为$phone的值,该如何实现?

------解决方案--------------------
PHP code
$phone=123;
$xmlDoc = new DOMDocument();
$xmlDoc->load("bb.xml");
$xml = new SimpleXMLElement($xmlDoc->saveXML());
$xml->Phone = $phone;
file_put_contents('bb.xml', $xml->asXML());
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code


<?php $xml = new DOMDocument("1.0","GB2312");//实例化domdocument类
$xml->load('test3.xml');//加载xml文件
$list=$xml->getElementsByTagName('Phone');
$phone='iphone';
$list->item(0)->nodeValue=$phone;  
$xml->save("test3.xml");//保存
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code

<?php require_once("xmlparse.php");    
$xml = new DOMDocument("1.0","GB2312");
$xml->load("你的xml的路径");
$list=$xml->getElementsByTagName('Phone');
$list->item(0)->nodeValue=$phone;  
$xml->save("你的xml的路径");//保存
?> <div class="clear">
                 
              
              
        
            </div>
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