php解析xml方法實(shí)例詳解
本文以實(shí)例形式詳細(xì)講述了php解析xml方法。分享給大家供大家參考。具體分析如下:
books.xml文件如下:
<?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
<?php
//創(chuàng)建一個(gè)DOMDocument對(duì)象
$doc=new DOMDocument();
//加載XML文件
$doc->load("books.xml");
//獲取所有的book標(biāo)簽
$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."<br>";
echo "author:".$author."<br>";
echo "year:".$year."<br>";
echo "price:".$price ."<br>";
echo "***********************************<br>";
}
?>
2、xml_parse_into_struct
創(chuàng)建解析器,將xml數(shù)據(jù)解析到數(shù)組,釋放解析器,再有就是從數(shù)組中提取想要的值。
<?php
// 讀取xml文件
$file = "books.xml";
$data = file_get_contents($file);
// 創(chuàng)建解析器
$parser = xml_parser_create();
// 將 XML 數(shù)據(jù)解析到數(shù)組中
xml_parse_into_struct($parser, $data, $vals, $index);
// 釋放解析器
xml_parser_free($parser);
// 數(shù)組處理
$arr = array();
$t=0;
foreach($vals as $value) {
$type = $value['type'];
$tag = $value['tag'];
$level = $value['level'];
$attributes = isset($value['attributes'])?$value['attributes']:"";
$val = isset($value['value'])?$value['value']:"";
switch ($type) {
case 'open':
if ($attributes != "" || $val != "") {
$arr[$t]['tag'] = $tag;
$arr[$t]['attributes'] = $attributes;
$arr[$t]['level'] = $level;
$t++;
}
break;
case "complete":
if ($attributes != "" || $val != "") {
$arr[$t]['tag'] = $tag;
$arr[$t]['attributes'] = $attributes;
$arr[$t]['val'] = $val;
$arr[$t]['level'] = $level;
$t++;
}
break;
}
}
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
3、用 SAX 解析器讀取 XML-----XML Simple API(SAX)解析器
<?php $file="books.xml"; $xml = simplexml_load_file($file); echo "<pre>"; print_r($xml); echo "</pre>"; ?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
- PHP 和 XML: 使用expat函數(shù)(一)
- PHP 和 XML: 使用expat函數(shù)(二)
- PHP 和 XML: 使用expat函數(shù)(三)
- PHP XML Expat解析器知識(shí)點(diǎn)總結(jié)
- PHP XML操作的各種方法解析(比較詳細(xì))
- php獲取通過(guò)http協(xié)議post提交過(guò)來(lái)xml數(shù)據(jù)及解析xml
- php的SimpleXML方法讀寫(xiě)XML接口文件實(shí)例解析
- php解析xml 的四種簡(jiǎn)單方法(附實(shí)例)
- php遍歷解析xml字符串的方法
- PHP用SAX解析XML的實(shí)現(xiàn)代碼與問(wèn)題分析
- php 使用expat方式解析xml文件操作示例
相關(guān)文章
PHP實(shí)現(xiàn)簡(jiǎn)單的新聞發(fā)布系統(tǒng)實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)簡(jiǎn)單的新聞發(fā)布系統(tǒng),涉及php實(shí)現(xiàn)新聞發(fā)布系統(tǒng)的sql查詢、插入、更新等完整操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
php和js實(shí)現(xiàn)根據(jù)子網(wǎng)掩碼和ip計(jì)算子網(wǎng)功能示例
這篇文章主要介紹了php和js實(shí)現(xiàn)根據(jù)子網(wǎng)掩碼和ip計(jì)算子網(wǎng)功能,結(jié)合實(shí)例形式分析了PHP與js針對(duì)IP地址子網(wǎng)掩碼計(jì)算的相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于php的命名空間與自動(dòng)加載實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
用php實(shí)現(xiàn)的下載css文件中的圖片的代碼
非常有創(chuàng)意的利用php獲取css中圖片地址并實(shí)現(xiàn)下載的代碼。2010-02-02
PHP提示Cannot modify header information - headers already sent
這篇文章主要介紹了PHP提示Cannot modify header information - headers already sent by解決方法,是在PHP程序開(kāi)發(fā)中非常典型的錯(cuò)誤情況,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-09-09

