濮阳杆衣贸易有限公司

主頁 > 知識庫 > PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】

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

熱門標簽:江西手機自動外呼防封系統(tǒng)是什么 哪里辦理400電話 怎么向銷售公司推銷外呼系統(tǒng) 仁和怎么申請400開頭的電話 外呼系統(tǒng)撥打暫時無法接通 廣州防封卡外呼系統(tǒng)多少錢一個月 廣東地市地圖標注 長春人工外呼系統(tǒng)服務商 高德地圖標注家

本文實例講述了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程序設計有所幫助。

您可能感興趣的文章:
  • PHP中使用DOMDocument來處理HTML、XML文檔的示例
  • PHP讀取XML文件的方法實例總結【DOMDocument及simplexml方法】
  • PHP基于DOMDocument解析和生成xml的方法分析
  • 如何解決php domdocument找不到的問題

標簽:廈門 海北 湘西 梅河口 黔東 惠州 文山 濮陽

巨人網絡通訊聲明:本文標題《PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】》,本文關鍵詞  PHP,創(chuàng)建,XML,的,方法,示例,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】》相關的同類信息!
  • 本頁收集關于PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】的相關信息資訊供網民參考!
  • 推薦文章
    若尔盖县| 疏勒县| 天峻县| 平昌县| 六枝特区| 甘孜| 四川省| 福州市| 铁力市| 肇东市| 白山市| 安多县| 枣庄市| 高安市| 忻州市| 思南县| 林西县| 灵川县| 卢氏县| 大方县| 蒲城县| 乐东| 周口市| 罗山县| 丹巴县| 特克斯县| 佛冈县| 台江县| 望谟县| 丹棱县| 阳朔县| 丽水市| 阳新县| 天祝| 武胜县| 辽中县| 林周县| 双峰县| 宁强县| 健康| 卢湾区|