首页  >  文章  >  后端开发  >  PHP透过DOM方式解析获得每本书的作者和书名

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

WBOY
WBOY原创
2016-06-13 11:16:041168浏览

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);
?>

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn