欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】

 更新時間:2019年09月10日 09:39:41   作者:koastal  
這篇文章主要介紹了PHP創(chuàng)建XML的方法,結合實例形式分析了php基于DOMDocument類及SimpleXMLElement類創(chuàng)建xml文件的具體步驟與相關實現技巧,需要的朋友可以參考下

本文實例講述了PHP創(chuàng)建XML的方法。分享給大家供大家參考,具體如下:

使用DOMDocument類創(chuàng)建xml

config.php

<?php
$doc = new DOMDocument('1.0','utf-8');
$doc->formatOutput = true;
//創(chuàng)建標簽
$mysql = $doc->createElement("mysql");
$host = $doc->createElement("host");
$username = $doc->createElement("username");
$password = $doc->createElement("password");
$database = $doc->createElement("database");
//創(chuàng)建標簽內容
$hostval = $doc->createTextNode("127.0.0.1");
$usernameval = $doc->createTextNode("root");
$passwordval = $doc->createTextNode("1234");
$databaseval = $doc->createTextNode("test");
//綁定標簽和內容
$host->appendChild($hostval);
$username->appendChild($usernameval);
$password->appendChild($passwordval);
$database->appendChild($databaseval);
//關聯(lián)標簽之間的關系
$doc->appendChild($mysql);
$mysql->appendChild($host);
$mysql->appendChild($username);
$mysql->appendChild($password);
$mysql->appendChild($database);
$doc->save("config.xml");

config.xml

<?xml version="1.0" encoding="utf-8"?>
<mysql>
 <host>127.0.0.1</host>
 <username>root</username>
 <password>1234</password>
 <database>test</database>
</mysql>

使用simplexml方法創(chuàng)建xml

config.php

<?php
$mysql = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><mysql></mysql>');
$host = $mysql->addchild("host","127.0.0.1");
$host->addAttribute("note","localhost");
$mysql->addchild("username","root");
$mysql->addchild("password","1234");
$mysql->addchild("database","test");
header("Content-type:text/xml;charset=utf-8");
echo $mysql->asXml();
$mysql->asXml("config.xml");

config.xml

<mysql>
<host note="localhost">127.0.0.1</host>
<username>root</username>
<password>1234</password>
<database>test</database>
</mysql>

PS:這里再為大家提供幾款關于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉換工具:
http://tools.jb51.net/code/xmljson

在線格式化XML/在線壓縮XML
http://tools.jb51.net/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP針對XML文件操作技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總

希望本文所述對大家PHP程序設計有所幫助。

相關文章

最新評論