PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】
本文實(shí)例講述了PHP創(chuàng)建XML的方法。分享給大家供大家參考,具體如下:
使用DOMDocument類創(chuàng)建xml
config.php
<?php $doc = new DOMDocument('1.0','utf-8'); $doc->formatOutput = true; //創(chuàng)建標(biāo)簽 $mysql = $doc->createElement("mysql"); $host = $doc->createElement("host"); $username = $doc->createElement("username"); $password = $doc->createElement("password"); $database = $doc->createElement("database"); //創(chuàng)建標(biāo)簽內(nèi)容 $hostval = $doc->createTextNode("127.0.0.1"); $usernameval = $doc->createTextNode("root"); $passwordval = $doc->createTextNode("1234"); $databaseval = $doc->createTextNode("test"); //綁定標(biāo)簽和內(nèi)容 $host->appendChild($hostval); $username->appendChild($usernameval); $password->appendChild($passwordval); $database->appendChild($databaseval); //關(guān)聯(lián)標(biāo)簽之間的關(guā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:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
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
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP針對(duì)XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP使用DOMDocument類生成HTML實(shí)例(包含常見標(biāo)簽元素)
- 如何解決php domdocument找不到的問題
- PHP中使用DOMDocument來處理HTML、XML文檔的示例
- PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】
- PHP基于DOMDocument解析和生成xml的方法分析
- PHP 中 DOMDocument保存xml時(shí)中文出現(xiàn)亂碼問題的解決方案
- php中DOMDocument簡單用法示例代碼(XML創(chuàng)建、添加、刪除、修改)
- PHP XML操作類DOMDocument
- php基于DOMDocument操作頁面元素實(shí)例
相關(guān)文章
PHP實(shí)現(xiàn)模仿socket請(qǐng)求返回頁面的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)模仿socket請(qǐng)求返回頁面的方法,是socket通信非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11Godaddy空間Zend Optimizer升級(jí)方法
雖然購買了godaddy的空間,期間也遇到很多的問題,在慢慢的額摸索中,也有的一些解決的方法。2010-05-05PHP獲取表單textarea數(shù)據(jù)中的換行問題
閑來無事,在網(wǎng)上看到一篇關(guān)于php表單轉(zhuǎn)換textarea換行符的文章,看完后,根據(jù)以往經(jīng)驗(yàn),感覺上這篇文章中的一些信息并不準(zhǔn)確...于是便自己親自對(duì)php獲取表單數(shù)據(jù)中的換行符問題進(jìn)行研究2010-09-09php模擬post行為代碼總結(jié)(POST方式不是絕對(duì)安全)
GET行為比較簡單,POST比較復(fù)雜一些2012-02-02