代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
读取XML
作者:
mickelfeng
/ 发布于
2013/3/18
/
606
books.xml文件: <books> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> <publisher>O'Reilly</publisher> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> <publisher>O'Reilly</publisher> </book> </books> 1.DOMDocument方法 <?php $doc = new DOMDocument(); $doc->load( 'books.xml' ); $books = $doc->getElementsByTagName( "book" ); foreach( $books as $book ) { $authors = $book->getElementsByTagName( "author" ); $author = $authors->item(0)->nodeValue; $publishers = $book->getElementsByTagName( "publisher" ); $publisher = $publishers->item(0)->nodeValue; $titles = $book->getElementsByTagName( "title" ); $title = $titles->item(0)->nodeValue; echo "$title - $author - $publisher\n"; echo ""; } ?> 2.用 SAX 解析器读取 XML <?php $g_books = array(); $g_elem = null; function startElement( $parser, $name, $attrs ) { global $g_books, $g_elem; if ( $name == 'BOOK' ) $g_books []= array(); $g_elem = $name; } function endElement( $parser, $name ) { global $g_elem; $g_elem = null; } function textData( $parser, $text ) { global $g_books, $g_elem; if ( $g_elem == 'AUTHOR' || $g_elem == 'PUBLISHER' || $g_elem == 'TITLE' ) { $g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text; } } $parser = xml_parser_create(); xml_set_element_handler( $parser, "startElement", "endElement" ); xml_set_character_data_handler( $parser, "textData" ); $f = fopen( 'books.xml', 'r' ); while( $data = fread( $f, 4096 ) ) { xml_parse( $parser, $data ); } xml_parser_free( $parser ); foreach( $g_books as $book ) { echo $book['TITLE']." - ".$book['AUTHOR']." - "; echo $book['PUBLISHER']."\n"; } ?> 3.用正则表达式解析 XML <?php $xml = ""; $f = fopen( 'books.xml', 'r' ); while( $data = fread( $f, 4096 ) ) { $xml .= $data; } fclose( $f ); preg_match_all( "/\<book\>(.*?)\<\/book\>/s", $xml, $bookblocks ); foreach( $bookblocks[1] as $block ) { preg_match_all( "/\<author\>(.*?)\<\/author\>/", $block, $author ); preg_match_all( "/\<title\>(.*?)\<\/title\>/", $block, $title ); preg_match_all( "/\<publisher\>(.*?)\<\/publisher\>/", $block, $publisher ); echo( $title[1][0]." - ".$author[1][0]." - ".$publisher[1][0]."\n" ); } ?> 4.解析XML到数 组 <?php $data = "<root><line /><content language=\"gb2312\">简单的XML数据</content></root>"; $parser = xml_parser_create(); //创建解析器 xml_parse_into_struct($parser, $data, $values, $index); //解析到数组 xml_parser_free($parser); //释放资源 //显示数组结构 echo "\n索引数组\n"; print_r($index); echo "\n数据数组\n"; print_r($values); ?> 5.检查XML是否有效 <?php //创建XML解析器 $xml_parser = xml_parser_create(); //使用大小写折叠来保证能在元素数组中找到这些元素名称 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true); //读取XML文件 $xmlfile = "bb.xml"; if (!($fp = fopen($xmlfile, "r"))) { die("无法读取XML文件$xmlfile"); } //解析XML文件 $has_error = false; //标志位 while ($data = fread($fp, 4096)) { //循环地读入XML文档,只到文档的EOF,同时停止解析 if (!xml_parse($xml_parser, $data, feof($fp))) { $has_error = true; break; } } if($has_error) { echo "该XML文档是错误的!"; //输出错误行,列及其错误信息 $error_line = xml_get_current_line_number($xml_parser); $error_row = xml_get_current_column_number($xml_parser); $error_string = xml_error_string(xml_get_error_code($xml_parser)); $message = sprintf("[第%d行,%d列]:%s", $error_line, $error_row, $error_string); echo $message; } else { echo "该XML文档是结构良好的。"; } //关闭XML解析器指针,释放资源 xml_parser_free($xml_parser); ?> 6.可用于精确的读取XML test.xml <?xml version="1.0" encoding="UTF-8" ?> <SBMP_MO_MESSAGE> <CONNECT_ID>100</CONNECT_ID> <MO_MESSAGE_ID>123456</MO_MESSAGE_ID> <RECEIVE_DATE>20040605</RECEIVE_DATE> <RECEIVE_TIME>153020</RECEIVE_TIME> <GATEWAY_ID>1</GATEWAY_ID> <VALID>1</VALID> <CITY_CODE>010</CITY_CODE> <CITY_NAME>北京</CITY_NAME> <STATE_CODE>010</STATE_CODE> <STATE_NAME>北京</STATE_NAME> <TP_PID>0</TP_PID> <TP_UDHI>0</TP_UDHI> <MSISDN>15933626501</MSISDN> <MESSAGE_TYPE>8</MESSAGE_TYPE> <MESSAGE>5618常年供应苗木,品种有玉兰、黄叶杨等。联系人:张三,电话:1234567890。</MESSAGE> <LONG_CODE>100</LONG_CODE> <SERVICE_CODE>9588</SERVICE_CODE> </SBMP_MO_MESSAGE> test.php: <?php $myData = array(); $file = file_get_contents("test.xml"); if(strpos($file, '<?xml') > -1) { try { //加载解析xml $xml = simplexml_load_string($file); if($xml) { //echo $this->result; //获取节点值 $CONNECT_ID = $xml->CONNECT_ID; $MO_MESSAGE_ID = $xml->MO_MESSAGE_ID; $RECEIVE_DATE = $xml->RECEIVE_DATE; $RECEIVE_TIME = $xml->RECEIVE_TIME; $GATEWAY_ID = $xml->GATEWAY_ID; $VALID = $xml->VALID; $CITY_CODE = $xml->CITY_CODE; $CITY_NAME = $xml->CITY_NAME; $STATE_CODE = $xml->CITY_CODE; $STATE_NAME = $xml->STATE_NAME; $TP_PID = $xml->TP_PID; $TP_UDHI = $xml->TP_UDHI; $MSISDN = $xml->MSISDN; $MESSAGE_TYPE = $xml->MESSAGE_TYPE; $MESSAGE = $xml->MESSAGE;//短信 $LONG_CODE = $xml->LONG_CODE; $SERVICE_CODE = $xml->SERVICE_CODE; preg_match("/(561)\d{1,2}/", $MESSAGE, $code); switch($code[0]) { case 5618 : $myData[message] = $MESSAGE; break; default : $myData[] = '没有短消息。'; break; } } else { echo "加载xml文件错误。"; } } catch(exception $e){ print_r($e); } } else { echo "没有该XML文件。"; } echo "<pre>"; print_r($myData); echo "<hr>"; echo $myData[message]; ?>
试试其它关键字
读取XML
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
mickelfeng
贡献的其它代码
(
22
)
.
安卓图片异步加载工具类
.
加密解密php代码
.
将PHP数组有格式的写入文件中
.
获取访问页面的http状态码
.
WinInet远程下载文件的
.
用NetAPI获取网卡的物理MAC地址
.
极方便的动态 SQL 生成工具
.
PHP图片缩放函数:实现等比例不失真缩放
.
判断手机移动设备访问
.
扁平化的bootstrap
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3