时间:2021-07-01 10:21:17 帮助过:3人阅读
具体如下:
<?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
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
PHP实现读取XML格式文件的方法
php通过eval实现字符串格式的计算公式
浅析json与jsonp区别及通过ajax获得json数据后格式的转换
以上就是php基于dom实现读取图书xml格式数据的方法详解的详细内容,更多请关注Gxl网其它相关文章!