Home  >  Article  >  Backend Development  >  PHP透过DOM方式解析获得每本书的作者和书名

PHP透过DOM方式解析获得每本书的作者和书名

WBOY
WBOYOriginal
2016-06-13 11:16:041164browse

PHP通过DOM方式解析获得每本书的作者和书名

  $doc = new DOMDocument('1.0','utf-8');
  $doc -> load('book.xml');
  $books = $doc ->getElementsByTagName('book');
  $first_book = $books ->item(0);
  $first_title = $first_book ->getElementByTagName('title')->item(0)->nodeValue;
  $first_author = $first_book ->getElementByTagName('author')->item(0)->nodeValue;
  $second_book = $books ->item(1);
  $second_title = $second_book ->getElementsByTagName('title');
  $tar = $title ->item(0);
  $text = $tar ->nodeValue;
  $third_book = $books ->item(2);
  $third_title = $third_book ->getElementsByTagName('title')->item(0)->nodeValue;
  $third_author = $third_book ->getElementsByTagName('author')->item(0)->nodeValue;
  var_dump($third_title);
  var_dump($third_author);
?>

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