php,解析xml
后端开发-php教程
本文以实例形式详细讲述了php解析xml方法。分享给大家供大家参考。具体分析如下:易语言反汇编源码,查看ubuntu su密码,手机能连tomcat,亚马逊爬虫理论,haxe 编译成php,seo srxlzw
books.xml文件如下:入门经典 源码,vscode 配置 npm,ubuntu终端代码更改字体,咕泡 tomcat,sqlite使用教程迅雷,微信插件论坛,中文前端框架模板下载,爬虫有哪些恶心,action php,seo综合分析,导航类的网站源码,网页视频通话源代码,动漫动作模板 JS,订单html页面,php用户vip管理系统源码,人人商城小程序生成分销二维码lzw
Harry Potter J K. Rowling 2005 29.99Everyday Italian Giada De Laurentiis 2005 30.00Learning XML Erik T. Ray 2003 39.95
证券软件源码,vscode为何不能选中注释,ubuntu界面很小,tomcat局域网端口,爬虫 异常 跳过,php 反射的应用,深圳百度seo优化服务lzw
1、DOM解析XMLload("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数据解析到数组,释放解析器,再有就是从数组中提取想要的值。
<?php // 读取xml文件 $file = "books.xml"; $data = file_get_contents($file); // 创建解析器 $parser = xml_parser_create(); // 将 XML 数据解析到数组中 xml_parse_into_struct($parser, $data, $vals, $index); // 释放解析器 xml_parser_free($parser); // 数组处理 $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 ""; print_r($arr); echo "";
?>3、用 SAX 解析器读取 XML-----XML Simple API(SAX)解析器
<?php $file="books.xml"; $xml = simplexml_load_file($file); echo ""; print_r($xml); echo "";
?>