Home > Backend Development > PHP Tutorial > Detailed explanation of how PHP reads book XML format data based on DOM

Detailed explanation of how PHP reads book XML format data based on DOM

墨辰丷
Release: 2023-03-28 07:16:01
Original
1074 people have browsed it

This article mainly introduces the method of using PHP to read book XML format data based on DOM. It involves PHP's DOM-based reading operation for XML format files. Friends in need can refer to the following

for details. 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

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

PHP implementation of reading XML format file

php implements the calculation formula of string format through eval

##A brief analysis of the difference between json and jsonp and obtaining json data through ajax PostFormat Conversion

The above is the detailed content of Detailed explanation of how PHP reads book XML format data based on DOM. For more information, please follow other related articles on 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