国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 php教程 php手冊(cè) php生成xml簡單實(shí)例代碼

php生成xml簡單實(shí)例代碼

Jun 13, 2016 pm 12:20 PM
php xml 代碼 基于 處理 實(shí)例 應(yīng)用 建立 開發(fā)者 生成 簡單 編碼 需要

當(dāng)處理基于XML應(yīng)用程序時(shí),開發(fā)者經(jīng)常需要建立XML編碼數(shù)據(jù)結(jié)構(gòu)。例如,Web中基于用戶輸入的XML狀態(tài)模板,服務(wù)器請(qǐng)求XML語句,以及基于運(yùn)行時(shí)間參數(shù)的客戶響應(yīng)。
盡管XML數(shù)據(jù)結(jié)構(gòu)的構(gòu)建比較費(fèi)時(shí),但如果使用成熟的PHP DOM應(yīng)用程序接口,一切都會(huì)變得簡單明了。本文將向你介紹PHP DOM應(yīng)用程序接口的主要功能,演示如何生成一個(gè)正確的XML完整文件并將其保存到磁盤中。
創(chuàng)建文檔類型聲明
一般而言,XML聲明放在文檔頂部。在PHP中聲明十分簡單:只需實(shí)例化一個(gè)DOM文檔類的對(duì)象并賦予它一個(gè)版本號(hào)。查看程序清單A:
程序清單 A

復(fù)制代碼 代碼如下:


// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// save and display tree
echo $dom->saveXML();
?>


請(qǐng)注意DOM文檔對(duì)象的saveXML()方法。稍后我再詳細(xì)介紹這一方法,現(xiàn)在你只需要簡單認(rèn)識(shí)到它用于輸出XML文檔的當(dāng)前快照到一個(gè)文件或?yàn)g覽器。在本例,為增強(qiáng)可讀性,我已經(jīng)將ASCII碼文本直接輸出至瀏覽器。在實(shí)際應(yīng)用中,可將以text/XML頭文件發(fā)送到瀏覽器。
如在瀏覽器中查看輸出,你可看到如下代碼:

添加元素和文本節(jié)點(diǎn)
XML真正強(qiáng)大的功能是來自其元素與封裝的內(nèi)容。幸運(yùn)的是,一旦你初始化DOM文檔,很多操作變得很簡單。此過程包含如下兩步驟:
對(duì)想添加的每一元素或文本節(jié)點(diǎn),通過元素名或文本內(nèi)容調(diào)用DOM文檔對(duì)象的createElement()或createTextNode()方法。這將創(chuàng)建對(duì)應(yīng)于元素或文本節(jié)點(diǎn)的新對(duì)象。
通過調(diào)用節(jié)點(diǎn)的appendChild()方法,并把其傳遞給上一步中創(chuàng)建的對(duì)象,并在XML文檔樹中將元素或文本節(jié)點(diǎn)添加到父節(jié)點(diǎn)。
以下范例將清楚地演示這2步驟,請(qǐng)查看程序清單B。
程序清單 B

復(fù)制代碼 代碼如下:


// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// save and display tree
echo $dom->saveXML();
?>


這 里,我首先創(chuàng)建一個(gè)名字為的根元素,并使它歸于XML頭文件中。然后,我建立名為的元素并使它 歸于根元素。最后,我又創(chuàng)建一個(gè)值為“pepperoni”的文本節(jié)點(diǎn)并使它歸于元素。最終結(jié)果如下:

復(fù)制代碼 代碼如下:




pepperoni


如果你想添加另外一個(gè)topping,只需創(chuàng)建另外一個(gè)并添加不同的內(nèi)容,如程序清單C所示。
程序清單C

復(fù)制代碼 代碼如下:


// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create another text node
$text = $dom->createTextNode("tomato");
$item->appendChild($text);
// save and display tree
echo $dom->saveXML();
?>


以下是執(zhí)行程序清單C后的輸出:

復(fù)制代碼 代碼如下:




pepperoni
tomato


添加屬性
通過使用屬性,你也可以添加適合的信息到元素。對(duì)于PHP DOM API,添加屬性需要兩步:首先用DOM文檔對(duì)象的createAttribute()方法創(chuàng)建擁有此屬性名字的節(jié)點(diǎn),然后將文檔節(jié)點(diǎn)添加到擁有屬性值的屬性節(jié)點(diǎn)。詳見程序清單D。
程序清單 D

復(fù)制代碼 代碼如下:


// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// save and display tree
echo $dom->saveXML();
?>


輸出如下所示:

復(fù)制代碼 代碼如下:




pepperoni


添加CDATA模塊和過程向?qū)?
雖然不經(jīng)常使用CDATA模塊和過程向?qū)?,但是通過調(diào)用DOM文檔對(duì)象的createCDATASection()和createProcessingInstruction()方法, PHP API 也能很好地支持CDATA和過程向?qū)?,?qǐng)見程序清單E。
程序清單 E

復(fù)制代碼 代碼如下:


// create doctype
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// for readability purposes
header("Content-Type: text/plain");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save and display tree
echo $dom->saveXML();
?>


輸出如下所示:

復(fù)制代碼 代碼如下:




pepperoni
Customer requests that pizza be sliced into 16 square pieces
]]>



保存結(jié)果
一旦已經(jīng)實(shí)現(xiàn)你的目標(biāo),就可以將結(jié)果保存在一個(gè)文件或存儲(chǔ)于PHP的變量。通過調(diào)用帶有文件名的save()方法可以將結(jié)果保存在文件中,而通過調(diào)用saveXML()方法可存儲(chǔ)于PHP的變量。請(qǐng)參考以下實(shí)例(程序清單F):
程序清單 F

復(fù)制代碼 代碼如下:


// create doctype
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("toppings");
$dom->appendChild($root);
$dom->formatOutput=true;
// create child element
$item = $dom->createElement("item");
$root->appendChild($item);
// create text node
$text = $dom->createTextNode("pepperoni");
$item->appendChild($text);
// create attribute node
$price = $dom->createAttribute("price");
$item->appendChild($price);
// create attribute value node
$priceValue = $dom->createTextNode("4");
$price->appendChild($priceValue);
// create CDATA section
$cdata = $dom->createCDATASection(" Customer requests that pizza be
sliced into 16 square pieces ");
$root->appendChild($cdata);
// create PI
$pi = $dom->createProcessingInstruction("pizza", "bake()");
$root->appendChild($pi);
// save tree to file
$dom->save("order.xml");
// save tree to string
$order = $dom->save("order.xml");
?>


下面是實(shí)際的例子,大家可以測(cè)試下。
xml.php(生成xml)

復(fù)制代碼 代碼如下:



$conn = mysql_connect('localhost', 'root', '123456') or die('Could not connect: ' . mysql_error());
mysql_select_db('vdigital', $conn) or die ('Can\'t use database : ' . mysql_error());
$str = "SELECT id,username FROM `admin` GROUP BY `id` ORDER BY `id` ASC";
$result = mysql_query($str) or die("Invalid query: " . mysql_error());
if($result)
{
$xmlDoc = new DOMDocument();
if(!file_exists("01.xml")){
$xmlstr = "";
$xmlDoc->loadXML($xmlstr);
$xmlDoc->save("01.xml");
}
else { $xmlDoc->load("01.xml");}
$Root = $xmlDoc->documentElement;
while ($arr = mysql_fetch_array($result)){
$node1 = $xmlDoc->createElement("id");
$text = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["id"]));
$node1->appendChild($text);
$node2 = $xmlDoc->createElement("name");
$text2 = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["username"]));
$node2->appendChild($text2);
$Root->appendChild($node1);
$Root->appendChild($node2);
$xmlDoc->save("01.xml");
}
}
mysql_close($conn);
?>


test.php(應(yīng)用測(cè)試)

復(fù)制代碼 代碼如下:



$xmlDoc = new DOMDocument();
$xmlDoc->load("http://localhost/xml/xml.php");
$x=$xmlDoc->getElementsByTagName('name');
for ($i=0; $ilength-1; $i++)
{
if(strpos($x->item($i)->nodeValue,"fang")!==false)
{
echo $x->item($i)->parentNode->childNodes->item(1)->nodeValue;
}
}
?>

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

為什么我們?cè)u(píng)論:PHP指南 為什么我們?cè)u(píng)論:PHP指南 Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

如何在Windows上安裝PHP 如何在Windows上安裝PHP Jul 15, 2025 am 02:46 AM

安裝PHP在Windows上的關(guān)鍵步驟包括:1.下載合適的PHP版本并解壓,推薦使用ThreadSafe版本配合Apache或NonThreadSafe版本配合Nginx;2.配置php.ini文件,將php.ini-development或php.ini-production重命名為php.ini;3.將PHP路徑添加到系統(tǒng)環(huán)境變量Path中以便命令行使用;4.測(cè)試PHP是否安裝成功,通過命令行執(zhí)行php-v和運(yùn)行內(nèi)置服務(wù)器測(cè)試解析能力;5.若使用Apache,需在httpd.conf中配置P

PHP語法:基礎(chǔ)知識(shí) PHP語法:基礎(chǔ)知識(shí) Jul 15, 2025 am 02:46 AM

PHP的基礎(chǔ)語法包括四個(gè)關(guān)鍵點(diǎn):1.PHP標(biāo)簽必須使用結(jié)束,推薦使用完整標(biāo)簽;2.輸出內(nèi)容常用echo和print,其中echo支持多參數(shù)且效率更高;3.注釋方式有//、#和//,用于提升代碼可讀性;4.每條語句必須以分號(hào)結(jié)尾,空格和換行不影響執(zhí)行但影響可讀性。掌握這些基本規(guī)則有助于寫出清晰穩(wěn)定的PHP代碼。

您的第一個(gè)PHP腳本:實(shí)用介紹 您的第一個(gè)PHP腳本:實(shí)用介紹 Jul 16, 2025 am 03:42 AM

如何開始編寫第一個(gè)PHP腳本?首先設(shè)置本地開發(fā)環(huán)境,安裝XAMPP/MAMP/LAMP,使用文本編輯器,了解服務(wù)器運(yùn)行原理。其次,創(chuàng)建一個(gè)名為hello.php的文件,輸入基本代碼并運(yùn)行測(cè)試。第三,學(xué)習(xí)混合使用PHP與HTML以實(shí)現(xiàn)動(dòng)態(tài)內(nèi)容輸出。最后,注意常見錯(cuò)誤如缺少分號(hào)、引用問題及文件擴(kuò)展名錯(cuò)誤,并開啟錯(cuò)誤報(bào)告以便調(diào)試。

什么是PHP,它是用什么? 什么是PHP,它是用什么? Jul 16, 2025 am 03:45 AM

PHPisaserver-sidescriptinglanguageusedforwebdevelopment,especiallyfordynamicwebsitesandCMSplatformslikeWordPress.Itrunsontheserver,processesdata,interactswithdatabases,andsendsHTMLtobrowsers.Commonusesincludeuserauthentication,e-commerceplatforms,for

PHP 8安裝指南 PHP 8安裝指南 Jul 16, 2025 am 03:41 AM

在Ubuntu上安裝PHP8的步驟為:1.更新軟件包列表;2.安裝PHP8及基礎(chǔ)組件;3.檢查版本確認(rèn)安裝成功;4.按需安裝額外模塊。Windows用戶可下載ZIP包并解壓,隨后修改配置文件、啟用擴(kuò)展并將路徑加入環(huán)境變量。macOS用戶推薦使用Homebrew安裝,依次執(zhí)行添加tap、安裝PHP8、設(shè)置默認(rèn)版本及驗(yàn)證版本等步驟。不同系統(tǒng)下安裝方式雖有差異,但流程清晰,根據(jù)用途選對(duì)方法即可。

您如何處理PHP中的文件操作(閱讀/寫作)? 您如何處理PHP中的文件操作(閱讀/寫作)? Jul 16, 2025 am 03:48 AM

tohandlefileoperationsinphp,useApprepreprunctions andModes.1.toreadafile,usefile_get_contents()forsmallfilesorfgets()inaloopforline by line-line-processing.2.towriteToafile,usefile_put_cte_contents(usefile_contents)(

python如果還有示例 python如果還有示例 Jul 15, 2025 am 02:55 AM

寫Python的ifelse語句關(guān)鍵在于理解邏輯結(jié)構(gòu)與細(xì)節(jié)。1.基礎(chǔ)結(jié)構(gòu)是if條件成立執(zhí)行一段代碼,否則執(zhí)行else部分,else可選;2.多條件判斷用elif實(shí)現(xiàn),順序執(zhí)行且一旦滿足即停止;3.嵌套if用于進(jìn)一步細(xì)分判斷,建議不超過兩層;4.簡潔場景可用三元表達(dá)式替代簡單ifelse。注意縮進(jìn)、條件順序及邏輯完整性,才能寫出清晰穩(wěn)定的判斷代碼。

See all articles