PHP读取XML格式文件的方法总结
程序员文章站
2024-03-13 18:15:21
本文实例总结了php读取xml格式文件的方法。分享给大家供大家参考,具体如下:
books.xml文件:
...
本文实例总结了php读取xml格式文件的方法。分享给大家供大家参考,具体如下:
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 "<br>"; } ?>
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文档是错误的!<br />"; //输出错误行,列及其错误信息 $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]; ?>
ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线xml/json互相转换工具:
在线格式化xml/在线压缩xml:
xml在线压缩/格式化工具:
xml代码在线格式化美化工具:
更多关于php相关内容感兴趣的读者可查看本站专题:《php针对xml文件操作技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php错误与异常处理方法总结》、《php基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。