Home >php教程 >php手册 >php解析xml方法实例详解

php解析xml方法实例详解

WBOY
WBOYOriginal
2016-06-06 20:04:161119browse

这篇文章主要介绍了php解析xml方法,以实例形式详细分析了php解析XML的相关技巧,需要的朋友可以参考下

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

books.xml文件如下:

Harry Potter J K. Rowling 2005 29.99 Everyday Italian Giada De Laurentiis 2005 30.00 Learning XML Erik T. Ray 2003 39.95

1、DOM解析XML

load("books.xml"); //获取所有的book标签 $bookDom=$doc->getElementsByTagName("book"); foreach($bookDom 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."
"; echo "author:".$author."
"; echo "year:".$year."
"; echo "price:".$price ."
"; echo "***********************************
"; } ?>

2、xml_parse_into_struct

创建解析器,,将xml数据解析到数组,释放解析器,再有就是从数组中提取想要的值。

"; print_r($arr); echo ""; ?>

3、用 SAX 解析器读取 XML-----XML Simple API(SAX)解析器

"; print_r($xml); echo ""; ?>

希望本文所述对大家的php程序设计有所帮助。

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