php基于dom實現(xiàn)的圖書xml格式數(shù)據(jù)示例
本文實例講述了php基于dom實現(xiàn)的圖書xml格式數(shù)據(jù)。分享給大家供大家參考,具體如下:
<?php
$books = array();
$books [] = array(
'title' => 'PHP Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$books [] = array(
'title' => 'Podcasting Hacks',
'author' => 'Jack Herrington',
'publisher' => "O'Reilly"
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "books" );
$doc->appendChild( $r );
foreach( $books as $book )
{
$b = $doc->createElement( "book" );
$author = $doc->createElement( "author" );
$author->appendChild(
$doc->createTextNode( $book['author'] )
);
$b->appendChild( $author );
$title = $doc->createElement( "title" );
$title->appendChild(
$doc->createTextNode( $book['title'] )
);
$b->appendChild( $title );
$publisher = $doc->createElement( "publisher" );
$publisher->appendChild(
$doc->createTextNode( $book['publisher'] )
);
$b->appendChild( $publisher );
$r->appendChild( $b );
}
echo $doc->saveXML();
?>
運行結果如下:
<?xml version="1.0"?> <books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> <publisher>O'Reilly</publisher> </book> </books>
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數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《PHP錯誤與異常處理方法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
Linux Apache PHP Oracle 安裝配置(具體操作步驟)
本篇文章是對在Linux下安裝Apache+PHP連接Oracle的具體操作步驟進行了詳細的分析介紹,需要的朋友參考下2013-06-06
php中call_user_func函數(shù)使用注意事項
這篇文章主要介紹了php中call_user_func函數(shù)使用注意事項,較為詳細的講述了call_user_func函數(shù)的用法實例與注意事項,具有一定的參考借鑒價值,需要的朋友可以參考下2014-11-11
Laravel框架使用技巧之使用url()全局函數(shù)返回前一個頁面的地址方法詳解
這篇文章主要介紹了Laravel框架使用技巧之使用url()全局函數(shù)返回前一個頁面的地址,需要的朋友可以參考下2020-04-04
php模擬socket一次連接,多次發(fā)送數(shù)據(jù)的實現(xiàn)代碼
php模擬socket一次連接,多次發(fā)送數(shù)據(jù)的實現(xiàn)代碼,需要的朋友可以參考下。2011-07-07

