国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php解析xml方法實例詳解

2024-05-04 23:35:08
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了php解析xml方法,以實例形式詳細分析了php解析XML的相關技巧,需要的朋友可以參考下

本文以實例形式詳細講述了php解析xml方法。分享給大家供大家參考。具體分析如下:

books.xml文件如下:

 

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

1、DOM解析XML

 

 
  1. <?php 
  2. //創建一個DOMDocument對象 
  3. $doc=new DOMDocument(); 
  4. //加載XML文件 
  5. $doc->load("books.xml"); 
  6. //獲取所有的book標簽 
  7. $bookDom=$doc->getElementsByTagName("book"); 
  8. foreach($bookDom as $book){ 
  9. $title = $book->getElementsByTagName("title")->item(0)->nodeValue; 
  10. $author = $book->getElementsByTagName("author")->item(0)->nodeValue; 
  11. $year = $book->getElementsByTagName("year")->item(0)->nodeValue; 
  12. $price = $book->getElementsByTagName("price")->item(0)->nodeValue; 
  13. echo "title:".$title."<br>"
  14. echo "author:".$author."<br>"
  15. echo "year:".$year."<br>"
  16. echo "price:".$price ."<br>"
  17. echo "***********************************<br>"
  18. ?> 

2、xml_parse_into_struct

創建解析器,將xml數據解析到數組,釋放解析器,再有就是從數組中提取想要的值。

 

 
  1. <?php 
  2. // 讀取xml文件 
  3. $file = "books.xml"
  4. $data = file_get_contents($file); 
  5. // 創建解析器 
  6. $parser = xml_parser_create(); 
  7. // 將 XML 數據解析到數組中 
  8. xml_parse_into_struct($parser$data$vals$index); 
  9. // 釋放解析器 
  10. xml_parser_free($parser); 
  11. // 數組處理 
  12. $arr = array(); 
  13. $t=0; 
  14. foreach($vals as $value) { 
  15. $type = $value['type']; 
  16. $tag = $value['tag']; 
  17. $level = $value['level']; 
  18. $attributes = isset($value['attributes'])?$value['attributes']:""
  19. $val = isset($value['value'])?$value['value']:""
  20. switch ($type) { 
  21. case 'open'
  22. if ($attributes != "" || $val != "") { 
  23. $arr[$t]['tag'] = $tag
  24. $arr[$t]['attributes'] = $attributes
  25. $arr[$t]['level'] = $level
  26. $t++; 
  27. }  
  28. break
  29. case "complete"
  30. if ($attributes != "" || $val != "") { 
  31. $arr[$t]['tag'] = $tag
  32. $arr[$t]['attributes'] = $attributes
  33. $arr[$t]['val'] = $val
  34. $arr[$t]['level'] = $level
  35. $t++; 
  36. }  
  37. break
  38. }  
  39. }  
  40. echo "<pre>"
  41. print_r($arr); 
  42. echo "</pre>"
  43. ?> 

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

 

 
  1. <?php 
  2. $file="books.xml"
  3. $xml = simplexml_load_file($file); 
  4. echo "<pre>"
  5. print_r($xml); 
  6. echo "</pre>"
  7. ?> 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 道真| 弥渡县| 梁山县| 海门市| 启东市| 孙吴县| 珲春市| 江津市| 佛冈县| 白水县| 宿州市| 吴桥县| 通州区| 沂水县| 荃湾区| 龙南县| 亚东县| 尼玛县| 育儿| 徐闻县| 寿阳县| 阳曲县| 南投市| 井冈山市| 虎林市| 南安市| 英超| 壤塘县| 嘉定区| 法库县| 曲阳县| 孟连| 南和县| 荣昌县| 宜丰县| 嘉鱼县| 洛南县| 玉林市| 肥乡县| 翁源县| 琼中|