- 相關(guān)推薦
php如何基于dom實現(xiàn)圖書xml格式數(shù)據(jù)
導(dǎo)語:php如何基于dom實現(xiàn)圖書xml格式數(shù)據(jù)呢?下面是小編給大家提供的代碼實現(xiàn)方法,大家可以參考閱讀,更多詳情請關(guān)注應(yīng)屆畢業(yè)生考試網(wǎng)。
<?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
【php如何基于dom實現(xiàn)圖書xml格式數(shù)據(jù)】相關(guān)文章:
PHP如何使用DOM和simplexml讀取xml文檔07-22
PHP如何使用curl實現(xiàn)數(shù)據(jù)抓取09-27
如何實現(xiàn)PHP獲取表單數(shù)據(jù)與HTML嵌入PHP腳本09-23
php如何實現(xiàn)驗證碼06-13
基于PHP+Ajax實現(xiàn)表單驗證的詳解08-22
PHP基于CURL進行POST數(shù)據(jù)上傳的方法06-19
php解析XML文檔屬性并編輯的代碼09-28