首页 > 网站 > 建站经验 > 正文

php解析xml方法,实例详解

2019-11-02 15:28:13
字体:
来源:转载
供稿:网友

   本文以实例形式详细讲述了php解析xml方法。分享给大家供大家参考。具体分析如下:

  books.xml文件如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>

  1、DOM解析XML

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?php //创建一个DOMDocument对象 $doc=new DOMDocument(); //加载XML文件 $doc->load("books.xml"); //获取所有的book标签 $bookDom=$doc->getElementsByTagName("book"); foreach($bookDom
搞笑动态图片笑死人[www.62-6.com/1/dttp/]
as $book){ $title = $book->getElementsByTagName("title")->item(0)->nodeValue; $author = $book->getElementsByTagName("author")->item(0)->nodeValue; $year = $book->getElementsByTagName("year")->item(0)->nodeValue; $price = $book->getElementsByTagName("price")->item(0)->nodeValue; echo "title:".$title."<br>"; echo "author:".$author."<br>"; echo "year:".$year."<br>"; echo "price:".$price ."<br>"; echo "***********************************<br>"; } ?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表