背景:
一个操作xml DOM模型的行之有效的小例子。
正文:
1. xml 文件
<books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly Jack Herrington Podcasting Hacks O'Reilly</publisher> </book> </books>
|
2. 结构图
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"; } ?>
|
操作XML就是这样简单,文章内容出自IBM技术文档。
阅读(491) | 评论(0) | 转发(0) |