php基于dom實現讀取圖書xml格式數據的方法
本文實例講述了php基于dom實現讀取圖書xml格式數據的方法。分享給大家供大家參考,具體如下:
<?php
$doc = new DOMDocument();
$doc->load( 'books.xml' );
$books = $doc->getElementsByTagName( "book" );
foreach( $books as $book )
{
$authors = $book->getElementsByTagName( "author" );
$author = $authors->item(0)->nodeValue;
$publishers = $book->getElementsByTagName( "publisher" );
$publisher = $publishers->item(0)->nodeValue;
$titles = $book->getElementsByTagName( "title" );
$title = $titles->item(0)->nodeValue;
echo "$title - $author - $publisher\n";
}
?>
books.xml文件如下:
<?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>
運行結果如下:
PHP Hacks - Jack Herrington - O'Reilly Podcasting Hacks - Jack Herrington - O'Reilly
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基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
相關文章
php實現的Curl封裝類Curl.class.php用法實例分析
這篇文章主要介紹了php實現的Curl封裝類Curl.class.php用法,以完整實例形式較為詳細的分析了Curl封裝類的定義及相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
PHP面向對象程序設計模擬一般面向對象語言中的方法重載(overload)示例
這篇文章主要介紹了PHP面向對象程序設計模擬一般面向對象語言中的方法重載(overload),結合具體實例形式分析了php模擬一般面向對象程序設計語言中方法重載的相關操作技巧與注意事項,需要的朋友可以參考下2019-06-06

