php解析xml方法實例詳解
本文以實例形式詳細講述了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)建一個DOMDocument對象
$doc=new DOMDocument();
//加載XML文件
$doc->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."<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>"; ?>
希望本文所述對大家的php程序設計有所幫助。
相關文章
一款簡單實用的php操作mysql數(shù)據(jù)庫類
這篇文章主要介紹了一款簡單實用的php操作mysql數(shù)據(jù)庫類,不但包含了php針對mysql數(shù)據(jù)庫的常見操作之外,還有針對危險字符的過濾功能,非常具有實用價值,需要的朋友可以參考下2014-12-12
約瑟夫環(huán)問題的PHP實現(xiàn) 使用PHP數(shù)組內(nèi)部指針操作函數(shù)
約瑟夫環(huán)問題相信大家都已經(jīng)很熟悉了,一直想使用 PHP 來實現(xiàn)一下,琢磨了老半天的時間終于弄出來了,也許沒有網(wǎng)上的一些代碼實現(xiàn)的簡潔高效,但是畢竟是寫出來了~呵呵。2010-10-10
PHP中filter函數(shù)校驗數(shù)據(jù)的方法詳解
這篇文章主要是介紹PHP中filter函數(shù)校驗數(shù)據(jù)的方法詳解,PHP過濾器包含兩種類型:Validation用來驗證驗證項是否合法 、Sanitization用來格式化被驗證的項目,因此它可能會修改驗證項的值,將不合法的字符刪除,需要的朋友可以參考下2015-07-07
解析php curl_setopt 函數(shù)的相關應用及介紹
本篇文章是對php中的curl_setopt函數(shù)進行了詳細的分析介紹,需要的朋友參考下2013-06-06
php下關于Cannot use a scalar value as an array的解決辦法
已經(jīng)定義過的一個布爾型變量在下面被我直接當數(shù)組來調(diào)用了,所以就出現(xiàn)錯誤了2010-08-08

