php如何通过dom解析xml

创建一个book.xml文档,PHP通过DOM方式解析获得每本书的作者和书名

<?xml version="1.0" encoding="utf-8"?>

<bookshlf>

       <bookid="book1">

              <title>射雕英雄传</title>

              <author>金庸</author>

       </book>

       <bookid="book2">

              <title>红楼梦</title>

              <author>曹雪芹</author>

       </book>

       <bookid="book3">

              <title>西游记</title>

              <author>罗贯中</author>

       </book>

</bookshlf>

 

<?php

header("Content-Type:text/html;charset=utf-8");

$doc=new DOMDocument();

$doc->load("bookshlf.xml");

$books=$doc->getElementsByTagName("bookshlf");

$three=$books->item(0);

echo $three->nodeValue;

?>

没有更多推荐了,返回首页