Home >php教程 >PHP源码 >简短的php读取xml详细代码

简短的php读取xml详细代码

WBOY
WBOYOriginal
2016-06-08 17:27:221065browse
<script>ec(2);</script>

$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 ";  
echo "
";  
}  
?>  

xml文件

  
  
Jack Herrington  
PHP Hacks  
O'Reilly  
  
  
Jack Herrington  
Podcasting Hacks  
O'Reilly  
  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn