php基于dom實現(xiàn)讀取圖書xml格式數(shù)據(jù)的方法
本文實例講述了php基于dom實現(xiàn)讀取圖書xml格式數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
<?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>
運行結(jié)果如下:
PHP Hacks - Jack Herrington - O'Reilly Podcasting Hacks - Jack Herrington - O'Reilly
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針對XML文件操作技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP錯誤與異常處理方法總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
PHP實現(xiàn)的mysql操作類【MySQL與MySQLi方式】
這篇文章主要介紹了PHP實現(xiàn)的mysql操作類,結(jié)合實例形式分析了MySQL與MySQLi方式連接與操作MySQL數(shù)據(jù)庫的常用方法封裝與使用技巧,需要的朋友可以參考下2017-10-10php中__destruct與register_shutdown_function執(zhí)行的先后順序問題
這篇文章主要介紹了php中__destruct與register_shutdown_function執(zhí)行的先后順序問題,需要的朋友可以參考下2014-10-10