PHP implements method of reading book XML format data based on DOM

高洛峰
Release: 2023-03-05 15:04:01
Original
1331 people have browsed it

The example of this article describes the method of reading book xml format data based on dom. Share it with everyone for your reference, the details are as follows:

<?php
 $doc = new DOMDocument();
 $doc->load( &#39;books.xml&#39; );
 $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";
 }
?>
Copy after login

books.xml file is as follows:

<?xml version="1.0"?>
<books>
 <book>
  <author>Jack Herrington</author>
  <title>PHP Hacks</title>
  <publisher>O&#39;Reilly</publisher>
 </book>
 <book>
  <author>Jack Herrington</author>
  <title>Podcasting Hacks</title>
  <publisher>O&#39;Reilly</publisher>
 </book>
</books>
Copy after login

The running results are as follows:

PHP Hacks - Jack Herrington - O&#39;Reilly
Podcasting Hacks - Jack Herrington - O&#39;Reilly
Copy after login

I hope this article will be helpful to everyone in PHP programming.

For more articles related to PHP's method of reading book XML format data based on DOM, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!