本文实例总结了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方法
<"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>"; } "htmlcode"><"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"; } "htmlcode"><""; $f = fopen( 'books.xml', 'r' ); while( $data = fread( $f, 4096 ) ) { $xml .= $data; } fclose( $f ); preg_match_all( "/\<book\>(.*", $xml, $bookblocks ); foreach( $bookblocks[1] as $block ) { preg_match_all( "/\<author\>(.*", $block, $author ); preg_match_all( "/\<title\>(.*", $block, $title ); preg_match_all( "/\<publisher\>(.*", $block, $publisher ); echo( $title[1][0]." - ".$author[1][0]." - ".$publisher[1][0]."\n" ); } "htmlcode"><"<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); "htmlcode"><"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); "htmlcode"><"1.0" encoding="UTF-8" "htmlcode"><"test.xml"); if(strpos($file, '<"/(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]; "color: #800000">PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:在线XML/JSON互相转换工具:
http://tools.jb51.net/code/xmljson在线格式化XML/在线压缩XML:
http://tools.jb51.net/code/xmlformatXML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compressXML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP针对XML文件操作技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
华山资源网 Design By www.eoogi.com广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!华山资源网 Design By www.eoogi.com暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 张学友.1999-有个人【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《古筝佛曲》1CD[MP3][411.1MB]
- 纯音入心系列纯音乐《古筝世界名曲》1CD[MP3][38.9MB]
- 纯音入心系列纯音乐《古筝新奏-菩提妙音》1CD[MP3][112.3MB]
- 陈思安.1993-怀念福建金曲5CD【歌丽美】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】
- 许美静.1999-快乐无罪【上华】【WAV+CUE】
- 群星.2024-Jambo.Medley(BEYOND40周年·致敬2)【Kinns】【FLAC分轨】
- 林翠萍.1991-舞曲世界2辑【星河】【WAV+CUE】
- 关智斌.2007-In.Progress【英皇娱乐】【FLAC分轨】
- 群星.2000-电影主题曲原声带·琼瑶的世界【歌林】【WAV+CUE】
- 范世錡.2024-錡妙夜(EP)【天娱传媒】【FLAC分轨】
- 谭咏麟.2000-魅力千禧演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 纯音入心系列纯音乐《器乐古筝》1CD[MP3][239MB]
- 纯音入心系列纯音乐《华夏民乐之古筝篇》1CD[MP3][192MB]