Heim > Artikel > Backend-Entwicklung > PHP透过DOM方式解析获得每本书的作者和书名
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);
?>